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.
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 a GWT application which has many firefox windows. I need functionality to close all these windows with a single click on a button. I know the window.close() function, but it only closes the current window.
I wonder if there is a firefox plugin or anything what can help me achieve this functionality.
In Firefox the File menu contains an Exit button what terminates the program, but i can't find a way to call this from javascript.
You cannot close other windows with script that didn't open them. But
var newWindow = window.open( url );
newWindow.close();
will open new tab with url and then you will be able to close it with newWindow.close();
When a new window is opened with window.open(), a reference to the new window is returned.
This means var window1 = window.open("example.com", "windowName"); will allow you to close the this new window later with window1.close();
Windows are often opened this way to address your problem, though if the windowName specified existed already, the existing window would be used to load the page instead, and the variable would refer to the existing window.
EDIT: This explains browser windows and associated window control JavaScript here
I am currently using Mozilla Firefox 30.0 and it seems that it doesn't support window.close() anymore.
You may refer on the image below or if it is too small for you, here is the
link.
I opened the website of Google using window.open and then I tried to close it using window.close() but it says undefined.
Is there any other option that I can using to close the window using javascript on firefox?
The Firebug console unfortunately does not display the warning that goes along with it, which reads (in the regular Firefox Web Console}:
Scripts may not close windows that were not opened by script.
Also MDN window.close states:
This method is only allowed to be called for windows that were opened by a script using the window.open method.
So, you aren't allowed to call window.close() on windows that were explicitly opened by the user.
PS: This isn't new behavior, but around for ages (even before Firefox was called Firefox).
PS: To give an example, you are allowed to do something like this, only, i.e. close a window returned from window.open:
var w = window.open("http://google.com");
setTimeout(function() { w.close(); }, 1000);
I don't know exactly the cause of this symptom but here goes. In our web app, we call window.open to open a new window/tab (internally everyone's is set to open in a new tab). After opening this new tab, IE8 immediately switches the focus to it.
We then integrated a web service, so we call this web service which in turn invokes a callback javascript function when it returns. We are now calling window.open from inside the callback function. When we do this, IE8 opens the new tab but does not switch the focus. Calls to window.focus on the parent page and the child page do nothing.
Now, I know javascript is not threaded, but is this something to do with asynchronicity? Is there a way to get the browser to behave as expected?
I believe that by default, IE will only switch focus to the new tab if it was opened as a result of a user-initiated action such as a click. If the window.open call is not in response to a UI action then IE is probably opening the tab in the background as if it were some kind of popup, though obviously it's not being blocked by the popup blocker.
If you are in an intranet environment and can specify settings, have you tried setting the "Always switch to new tabs when they are created" check box on the tabbed browsing settings dialog?
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().