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

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(....);
}

Related

how to set document title of new open window?

I am doing as below code to set document title for new open window
var downloadWindow = window.open("https://www.google.com/", "_blank");
downloadWindow.document.title = "my title";
If we try to execute above snippet in console in google chrome, we can see while loading, We can see "my title", but same behaviour I want to be in internet explore?
How we can achieve same behaviour in internet explore?
Try this
var downloadWindow = window.open("https://www.google.com/", "_blank");
downloadWindow.document.write('<title>my title</title>');
I think that due to security reasons, you will not able to see the modified title on the newly open window.
As a workaround, I suggest you try to display the alert message that informs the user that he is opening the downloading window.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script>
alert("You are moving to download window...");
var downloadWindow = window.open("https://example.com", "_blank");
</script>
</head>
<body>
Test page
</body>
</html>
Let us know if you have any further questions. We will try to provide suggestions for it.

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>

IE11 on WIN 8.1 : window.open with the same name is opening new popup window

When running the below HTML page it is opening two popup windows. It is not the case with previous versions of IE. When window.open call is made with the same window name that has already opened, it should return the reference to the opened window. But in IE11 it is opening another new window.
<html>
<head>
<script type="text/javascript">
window.open("http://www.google.com", "test", "resizable=no,scrollbars=yes,width=260,height=225");
window.open("http://www.google.com", "test", "resizable=no,scrollbars=yes,width=260,height=225");
</script>
</head>
</html>
This behavior is not happening, if you not mentioned a url. (See below html page). Below html page is launching only one popup window.
<html>
<head>
<script type="text/javascript">
window.open("", "test", "resizable=no,scrollbars=yes,width=260,height=225");
window.open("", "test", "resizable=no,scrollbars=yes,width=260,height=225");
</script>
</head>
</html>
When we provide url or navigate to url window is losing its name. If you try to get the popup window name using window.name it is returning empty string.
It is happening only in Windows 8.1 and IE 11 and not happening in Windows 7 and IE11.
Are there any tweaks I need to do to make this work in IE11?
Update:
It seems to be IE bug. IE team is investigating it.
https://connect.microsoft.com/IE/feedback/details/797964/ie11-win-8-1-window-open-with-the-same-name-is-opening-new-popup-window#details
Update 2:
It is happening only if Internet Explorer runs in elevated mode.
Please try this.. this will check the window is still open or closed..
<script type="text/javascript">
var myWindow = null;
if(!myWindow || (myWindow && myWindow.closed)){
myWindow = window.open("http://www.google.com", "test", "resizable=no,scrollbars=yes,width=260,height=225");
}
else{
myWindow.location.href = 'http://www.google.com';
}
</script>

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>

Javascript redirect - New Window

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

Categories