Javascript redirect - New Window - javascript

I'm trying to create a re-direct using Javascript from inside a blank iframe that will direct to a URL inside a new window or tab.
More specifically, I'm trying to make a Facebook tab point to my company's webpage rather than load the page inside the tab's iframe which is not the same width as our web page.
Here is the redirect script I already have but it doesn't open the URL in a new window.
<script language="JavaScript">
<!--Redirect to 75th page
var time = null
function move() {
window.location = 'http://philmontscoutranch.org/Camping/75.aspx'
}
timer=setTimeout('move()',0000)
//-->
</script>
Ok - This snippet will open a new window when the page loads but Chrome's pop-up blocker blocked it. I didn't think about this until now. Maybe I could have it load in a light window?
<script language="JavaScript">
<!--Redirect to 75th page
var time = null
function open_win()
{
window.open("http://philmontscoutranch.org/Camping/75.aspx", "_blank");
}
timer=setTimeout('open_win()',0000)
//-->
</script>

Here you go: Jsfiddle Example
function opennewtab(url){
var win = window.open(url, '_blank');
}

<html>
<head>
<script>
function open_win()
{
window.open("http://philmontscoutranch.org/Camping/75.aspx", "_blank");
}
</script>
</head>
<body>
<input type="button" value="Open Window" onclick="open_win()">
</body>
</html>
Source

Related

window.open Named Window is opening new window on each click

On launching a window using window.open(), a new instance of browser is getting launched on button click every time, even if the calling function is having named window.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function OpenNamedWindow() {
var w2 = window.open("http://stackoverflow.com", "myWindow", "width=500px;height=500px");
}
</script>
<input type="button" onclick="OpenNamedWindow();" value="Launch App" />
</body>
</html>
After launching the page when I try to access window.name, it shows empty.
I did some search and looks like cross domain is causing the issue.
How can I load the page in same instance instead of launching a new browser window all the time?
This is a trick
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
But even this, perhaps should not work in all browser depending on users configuration
This topic is still here in stackoverflow.
try to check it by yourself at the following link
Open a URL in a new tab (and not a new window) using JavaScript
It opens only one window at a time:
var window_name = null; // global
...
function open_new_window() {
if (window_name && !window_name.closed) {
window_name.close();
window_name = window.open(....);
}

Open new popup chrome window without tab section

Sometimes surfing the web with chrome,I encounter a website or web app,when click on an <a> or <button> element it opens a new popup browser window which does not have tab section while I do not have any Chrome Extension installed:
main window:
popup new window:
My question is,How to do this using JavaScript?
Thanks.
Please see tutorial here: http://www.w3schools.com/jsref/met_win_open.asp
<script>
function myFunction() {
window.open("http://www.w3schools.com","_blank",
"toolbar,scrollbars,resizable,top=500,left=500,width=400,height=400");
}
</script>
<!DOCTYPE html>
<html>
<body>
<button onclick="openTheWindow()">open window</button>
<script>
function openTheWindow() {
var myWindow = window.open("", "", "width=800,height=800");
}
</script>
</body>
</html>

why can't I get the content of another window?

I am a javascript beginner . I wrote a javascript code to open a website in new window and then get the content of that new window and display it in the first window .. and this is the code :
<!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var newwin=window.open("http://www.w3schools.com");
newwin.onload=function ()
{display.innerHTML = newwin.document.documentElement.innerHTML;};
</script>
</body></html>
It didn't work .. the web site opened in the new window but the content didn't appear in the first window .. why?
thanks in advance.
Use _parent as a second parameter to position your webpage. (BTW I am assuming you are allowing popups in your browser settings)
var newwin=window.open("http://www.w3schools.com", "_parent");
You cannot do this if the child window is from a different domain. Each domain is sandboxed from the others for security reasons.
There are 2 errors that prevent you:
Different domains
Even if same domain, you set onload function after calling window.open(). So the new "onload" behavior never runs
Solution for same domain:
<!DOCTYPE html>
<html><head></head><body>
<div id="display"></div>
<script type="text/javascript">
var w=window.open("another.html");
var tid = setInterval function () {
if (w.document.readyState !== 'complete')
return;
clearInterval(tid);
display.innerHTML = w.document.documentElement.innerHTML);
}, 100);
</script>
</body></html>

How to call a function when close popup window

I am calling the Javascript window.open() function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks() function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
// Add this event listener; the function will be called when the window closes
myWindow.onbeforeunload = function(){ alert("Thanks");};
myWindow.focus();
}
Try this!
You can swap out the function(){ alert("Thanks");}; for MyThanks to call your function.
The new window that you open will have an attribute called opener that references the Window object that created it. You can then call functions on the parent window by calling window.opener.MyThanks();
Popup:
<script type="text/javascript">
function closePopup() {
window.opener.MyThanks();
window.close();
}
</script>
<div>
<h1>My Popup</h1>
<!-- Form or info to display -->
<button onclick="closePopup();">Close</button>
</div>
While there is nothing inherently wrong with browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.
There are a bunch of ways to create dialogs within your webpage. jQuery UI is one library that I like.
You can try this:
<script>
function openWin() {
myWindow = window.open('http://facebook.com/ekwebhost', '', 'width=600,height=400,left=200');
myWindow.focus();
MyThanks();
}
function MyThanks() {
alert("Thanks");
}
</script>

Replace the content of _top window with content of iframe without reload page

Is it possible to replace the content of the browser window (_top frame) with the document loaded in an iframe, without issuing a new GET to load that document?
Something like this:
<html>
<head>
<script type="text/javascript" language="JavaScript">
$(document).ready(function() {
$("#link").click(function() {
// should present in the browser window the content of the iframe WITHOUT reload
// even preserve javascript/head code
$(document).html($("#frameDestination").html());
});
});
</script>
</head>
<body>
<a id="link" href="#">full window</a>
<iframe id="frameDestination" src="http://www.some-random-site.com" width="900px" height="500px"></iframe>
</body>
</html>
Use contents() when working with an iframe in jQuery.
$("#YourIframe").contents().find("html").html();
This is only going to work if the iframe is in the same domain as the parent.
I haven't tested cross domain, but on the same domain I have this script working:
$(document).ready(function() {
$("#link").click(function() {
var iframeBody = document.getElementById("frameDestination").contentDocument,
head = iframeBody['head'].innerHTML,
body = iframeBody['body'].innerHTML;
$('body').html( body );
$('head').html( head );
});
});
In the latest Firefox, I'm even able to set a variable in a script tag in the frame, and see that value in _top after clicking the link.

Categories