In IE7, a child window opened with window.open can close itself using window.close(), but a window opened with <a href=... target=_blank> will show a security warning if the child window tries to close itself.
In my application, I don't know how my child window is opened, and I need to know (in the child window JavaScript code) whether I can use the window.close() or not. Is there a way? Another way to ask the question is - is there a way in IE to differentiate between a window opened via window.open vs a window opened via target=_blank.
I tried checking window.opener but in both cases, there is a value there, so this does not allow me to differentiate between the two cases.
Try comparing window.opener and window.self
Source: Close window without the prompt message in IE7
This is how to avoid the prompt according to the page above:
function WinClose(){
window.open('','_self','');
window.close();
}
Close
Is this a possible approach for your page?
Just a blind shot, but you can try removing the onunload event in the window, in case it is there.
If you have control over the window.open events, you could give the new window a name (2nd parameter I think). You can then check for that name before applying window.close().
Related
Each time I try to use window.close(); I just get the error Scripts may only close the windows that were opened by them, is there any way to bypass this?
Looks like it’s not.
See the documentation:
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.
See also window.close and self.close do not close the window in Chrome.
I have Ie 11 to support. I open the popup using var myWindow = window.open('url','name','width=640,height=480,menubar=no,toolbar=no');
however, the handle returned (myWindow) is always null in ie.
I need to close that popup after certain events happen.
I know that it is possible because I've seen other sites do that in the same browser.
Any ideas?
If window.open() is returning null, then something is wrong. You're likely blocking popups, which means that the Window object will not be created (and thus myWindow will be null).
Check your security settings there and enable them.
Once you get window.open() to execute successfully, you can call myWindow.close() to close the popup when necessary. Note that the close() method can only close popups that have been opened using the window.open() method.
Is it possible to automatically close a web browser window (e. g., in Google Chrome) when the user clicks outside the window? In order to do this, there would need to be some method for detecting whether or not the window was focused, and also a method for closing the window.
Most current browsers only let you close the child windows of a parent.
You can't close the master parent window through a script.
By default most browsers do not allow javascript to close windows that were not opened by javascript itself. But if you use e.g. window.open(...) you can close that window with window.close().
So if you have a window opened by javascript you can do the following:
window.addEventListener('blur', function(){window.close();}, false);
Is there a way to find if browser pop-up window opened with window.open had the parameter 'scrollbars=yes'?
I need to run specific code depending on scrollbars being on or off, meaning they might not be showing but they are still on.
Even if there is not general browser solution, any help for IE will be great.
Paul
I would try finding the code that handles the window.open and saving a global variable. Then you can access the global variable by using window.parent
Try:
window.scrollbars.visible
This should return true or false.
See https://developer.mozilla.org/en/DOM/window.scrollbars
We have written a small javascript function which checks if a URL should open in same window or a popup. In cases when a url should open in a new window IE is giving some strange behaviour a window popup flashes and closes with a beep sound. Can anybody suggest whats going behind the scenes i do not think its my javascript which is wrong. Is it some browser weird behaviour.
I suspect you have a third party pop-up blocker installed.
Yep, this sounds like some third party stuff. By default IE will try to guess what it should do with popups (open in a popup window, open in a new tab, don't open at all) but it won't try to open a window and close it immediately (unless caused by some addons or code included in the page). JavaScript errors shouldn't cause the window to close either (unless they really call window.close()).