javascript window.open not opening in the same window internet explorer - javascript

I've currently have an issue with internet explorer 10 when calling window.open with same window name.
When calling window.open multiple times with the same name then multiple windows are opening instead of the page loading in the same window.
window.open("http://www.example.com","windowname","location=0,menubar=0,height=596,width=792,toolbar=0,scrollbars=1,status=0,resizable=1,left=0,screenX=0,top=0,screenY=0");
After some investigation of the user setting (which is a default setup for the company) there is list of sites in the trusted sites, when the domain www.example.com is added to the list of trusted websites then the page loads in the same window.
I've tried assigning the result of window.open to a variable and then calling window.close() before opening a new popup window but this causes a cross domain error.
var variable1 = window.open("http://www.example.com","windowname","location=0,menubar=0,height=596,width=792,toolbar=0,scrollbars=1,status=0,resizable=1,left=0,screenX=0,top=0,screenY=0");
variable1.close();
Can anyone offer an insight into which user setting causes this to happen? and how I always open the popup page in the same window?

Related

window.close fails when clicking links within opened window

I am opening a new window with window.open(). If I do NOTHING else on this page, and click the "close window" link, the window closes. This works perfectly; however, if I navigate between pages(all under the same domain) window.close() no longer works.
Is there a way to fix this?
Here is how I am opening pages in this example...
<a href="###" target="_blank">
Here is my close link:
close
I use 2 methods of changing pages within the opened windows.
<select onchange="if (this.value) window.location.href=this.value"> AND STANDARD <a href="####"> tag
Do I need to navigate links within this window a certain way to still maintain my window.close() ability?
You should favour using window.open() to open a new window if you want to close it using window.close(). E.g.
<script>
function newWindow() {
window.open('foo.html', 'myWindow');
return false;
}
</script>
link
From MDN:
The Window.close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.
I understand the behaviour you're describing where you can close the window as long as you don't navigate. I can replicate this in Google Chrome.
I believe this is because (from the spec):
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.
Your new window is considered a "top-level browsing context", not an "auxilliary browsing context". Up until the point that you navigate, the history contains one document, so can be closed using window.close(). As soon as you navigate, the history has more than one document so it's not valid to close the window.
You should also have a read about window.opener and the security risks it poses.
window.opener provides a reference to the window object that opened the current window.
Recent advice is to use rel="noopener" on all links that open in new windows. Again, this is because setting window.opener without knowing what you're doing poses a security risk. Have a read about it here:
Links to cross-origin destinations are unsafe
Browsers are now starting to treat all target="_blank" links as if rel="noopener" had been set, even if you as a developer don't set it. You can see that this is now in most major browsers.
Therefore, you could use link (explicitly setting window.opener) and I think you'd get the behaviour you want. However, this might not be reliable across browsers, and also has security implications as described in the web.dev article.

How to get idle time of a window.open() window?

As soon as the page loads, a window pops open using
var pop_window = window.open("third party site url".......)
My requirement is to -
1) close the window when the popup window is inactive.
My question - the window can be closed using pop_window.close(). but how to check for inactivity of the window when I only have the handler "pop_window"?
You can't you have to have the javascript you need in the new window you open, and if its different domain then you totally can't control or even close,
and even window.close() is not working well for all browsers, it may work for Chrome but not for Firefox, Try it cross browsers before you use in production.

IE10 Blocker Window.Open() Issue

I have a problem only affecting IE and no other browser (what's new?)
I'm using jquery-terminal (http://terminal.jcubic.pl) here where the user can input a command into a prompt and based on their command a window will popup and take them to a specified URL.
Eg: If a user types google into the command prompt it should open a new window and take them to www.google.com
if (command.match(/^\s*google\s*$/i)) {
window.open('http://www.google.com', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');
else if ... bing, yahoo, etc.
Okay, so IE will open the window and load the URL the first time without any hiccup if the user types 'google'. The problem arises when the user closes that window and goes to type in 'google' again, the window loads with a blank page instead of going to the URL.
So I ask, is there a way to fix this via code, or is IE trying to protect the user in its own way (ie. Settings problem?).
Thanks in advance.

Open URL in same window from different tabs

Let's say I have this function in a javascript on my web page that loads a url from an textfield with id url:
function loadUrl()
{
var url = document.getElementById('url').value;
window.open(url, 'urlwindow');
}
If I open two tabs in my browser and opens my web page in both tabs, I want both tabs to open the url in the same window, not two separate windows. Is this possible?
In other words, I want to target window opened from another tab.
You can't control the behaviur of the user's browser: just add as target windows "_blank" and hope your user have enough recent web client that opens a new tab and not a new window.
In my experience, this works perfectly (and just like you're trying to do it) with Firefox and IE, but not with Chrome or Safari.
Firefox and IE seem to use domain-specific namespaces for target names, while Chrome and Safari seem to use browser tab-specific namespaces for target names, so in these two browsers, 'urlwindow' as seen from tab A is different from 'urlwindow' as seen from tab B.
I'm trying to find a workaround for this myself, but have not yet been successful. See Chrome/Safari: how to open the same window from different tabs or windows

window.opener issue with window.open() when replacing a window

I have a web application where page #1 opens a popup window using
window.open(myUrl, "fixedApplicationTargetId", "");
Then page #2 overwrites the same popup window with a call to window.open using the same target value
window.open(anotherUrl, "fixedApplicationTargetId", "");
At this point the content of the popup originally created by page #1 shows the new content created by page #2. So far so good with any browser.
Then the popup itself detects who last opened the popup and updated the content using window.opener. Prior to calling window.open both page #1 and page #2 create a global variable globalPageId and assign a unique number each. The popup checks the value of window.opener.globalPageId and detects which window last updated the popup content.
This is where things fall apart: the above works fine with chrome and firefox that update window.opener in the popup each time the content is updated with window.open. Instead, IE and opera always point the popup window.opener to the first window that used window.open.
Any suggestion, in a context where multiple pages call window.open on the same target, how to detect from the popup itself which window last opened the window?
window.opener is supposed to be read-write (except in Internet Explorer 3), so you could set it to the appropriate window yourself. Some browsers, however, restrict this operation and only allow setting opener to null to prevent security issues.
An alternate solution would be to use a custom property instead of opener. You could set it by hand:
window.open(myUrl, "fixedApplicationTargetId", "").realOpener = window;
Then use window.realOpener.globalPageId instead of window.opener.globalPageId in the rest of your code.

Categories