Close Window in Javascript
I need 开发者_开发百科a way to close the browser (not a pop up) using Javascript. Is it possible?
If the window isn't opened by some JavaScript code from your own domain, then you can't close it in a crossbrowsercompatible way using JavaScript. Only in MSIE you can use window.close()
, but this will generate a warning which is however workaroundable by calling window.open('', '_self', '');
beforehand. Still, this won't work in Firefox and other decent browsers. And with a good reason. You shouldn't forcibly close windows which the enduser opened itself. This is Bad User Experience.
you can use this -
< a href="javascript:window.opener='x';window.close();">Close< /a>
usually when you use window.close() on main browser window, it shows a message saying "Page is trying to close window". To avoid this, set the window.opener to any value. Idea is window.opener should not be null or empty.
The whole browser? Generally, no. You can close the current tab with window.close()
, but it will prompt the user before closing the browser (unless it was created with window.open()
). This is a security feature; it would be unpleasant if an arbitrary webpage could close your browser unbidden.
In general, you can close a window that you yourself created using javascript.
You can't close any other window, not even the window in which your page is loaded. I remember that some browser actually ask "This web page is try to close your window. Allow?" or something of this sort. This is not something that you want your end user to see.
BTW, if you specify your particular use case, I could suggest alternate solutions.
try this,
<a href="" OnClick="window.close()">[CLOSE]</a>
It'll close window for sure
精彩评论