Some browsers will not allow you to close the window using window.close() unless the script opened the window. This is a little annoying sometimes. But there is a workaround to resolve this issue. If you observe the error message that is thrown by Mozilla Firefox, "Scripts may not close windows that were not opened by the script", it clearly says that if the script didn't open the window, you can't close that. But we open a blank page in the same window using "_self" as the target window and close the same window. In that way, the script opens the window (which is a blank one) and closes the window too.
So, to put this in the code,
<script> function closeMe() { var win = window.open("","_self"); /* url = "" or "about:blank"; target="_self" */ win.close(); } </script> <input type="button" name="CloseMe" value="Close Me" onclick="closeMe()" />
For example In mozilla, if there are more than one tabs open, then only the tab where this script is being executed, will be closed, not the whole window. But if there is only one tab open where this script is executing then the window will be closed.
Tested this script in Mozilla Firefox 1.5.0.4 and IE 6, XP SP2, Mozilla 1.7.12. I didn't test in any other browser.
Thankyou
Your Most Welcome!
I tried to send a reply: “Super!” but you have put up the barriers by insisting we fill in other boxes, email and name, when many like me never do that because we can’t be bothered. However as you see I have done it for you because you are the only site I found that has a solution AND your explanation was so clear and works exactly as you stated. Only slight misgiving is that the double quotes you use do not copy/paste into Notepad++ so I had to manually replace them (about ten seconds work with Notepad++!). Thank you.
Doesn’t work on FF 22.0 and Opera 12.16.
Tested and works on Chrome 28.0 and IE 7,8.
Hi Petar,
I am not sure for all latest browsers. these was working fine in all old browsers
Great Workaround …
Your Most Wecome!
this is not enough
Hi Ghani,
Let me know what isse are coming so i can help you
Literally spent about an hour and a half trying to find a workaround and then I saw this…thank goodness
Hi Mason,
Thanks for Comment and visit my blog!
Thanks
Raghunath
NOT WORKING… !! Tested on Chrome 30, FireFox 24
Delete the above comment, I am looking for closing Parent Window from iframe…
An interesting discussion is definitely worth comment.
I do think that you should publish more about this subject matter, it may not be a taboo subject but generally people don’t talk about such issues.
To the next! Kind regards!!
Very nice post. I certainly love this website. Thanks!
Code not display on Mozilla, opera. I want code that work on any browser.
Hello Raghunath,
Your and other solution worked if i have one tab and click on close me button but i when you redirect page to other page let say default.aspx one page and default1.aspx is other page and close button is on first page and other button for redirect to 2nd page and when come back to first page that close me not work what is problem is their any setting to chrome because it work in Mozilla and IE also .
Thanks
Hi,
I am here little confuse so can you please explain your problem in details.
Thanks
Raghunath
Actually problem is that let say i have three pages one is login.aspx and other is mainpage.aspx and logout.aspx. When i click on logout button on mainpage i am redirect to logout.aspx page and on logout page i have relogin and exit button that exit button is not working in chrome as it is not open in window.open as other says. reply if have need more detail about my problem
Thanks for quick reply!
So problem is only coming in Google chrome?
hi everyone does anyone have solution for my problem that is i want to disable current tab using javascript plz give solution ASAP
thanks in advance
Pradeep