Close Window in IE from javascript. (Small Hack)

When trying to close an window from javascript in IE, which is opened manually,
a security warning “The web page you are viewing is trying to close the window. Do you want to close this window? Yes|No ” is popping up. Is there any possibility to close a window silently? Yes.

A simple hack i found on the web will help to get rid of this situation.


this.focus();
self.opener = this;
self.close();

but in Firefox you can do this. Firefox will not allow you to close using the above code. Maybe anybody found/having any code post it here.

128 Replies to “Close Window in IE from javascript. (Small Hack)”

  1. Thanks I needed this to open my web application without the toolbars. My default.htm page is now able to open the aplication window and then close itself without the warning.

    Excellent!

  2. Program Files\Mozilla Firefox\greprefs\all.js must be edited to allow this to work. The pref(“dom.allow_scripts_to_close_windows”, false); needs to be set to true.

  3. Try this, for an all in one solution:

    if(navigator.appName==’Microsoft Internet Explorer’) {this.focus();self.opener = this;self.close();} else {window.open(”,’_parent’,”);window.close();}

    Doesn’t work in IE7 though, still get the popup there.

  4. To all who would like a Javascript solution that will close the browser window using FireFox, you should us JL’s solution above, however, it won’t work unless you fix the invalid characters in the script. They are only minor errors but still big enough to cause the script not to run.

    Try the fixed version of the script below:

    if(navigator.appName=="Microsoft Internet Explorer") {
    this.focus();self.opener = this;self.close(); }
    else { window.open('','_parent',''); window.close(); }

    All that is different is the apostrophes around the “Microsoft Internet Explorer” text (‘ ‘) have been changed to quotation marks (” “). There is also another stray character in the line:

    window.open('?,'_parent?,'?);

    If you can see, it appears just after the apostrophe, after the first open bracket.

    Anyway, I hope that fixes any problems for people using that solution. Oh, and just a note on browser compatibility. I have tested this script in Internet Explorer 6, Mozilla FireFox 1.5 and Opera 8.51 – they worked perfectly in all browsers.

    Regards,
    Becky

  5. In IE7, If we opened window with target=”_blank”, the window.close() now gets this warning message. Anyone know if this will be fixed or do we have to use js to open the popup window?
    thanks.

  6. Pete,
    You have to use different window names to prevent the pages from opening in the same window. Here is the code i’ve modified below

    function openPopup(winname,purl)
    {
    wndAttr = "width=500,height=400,left=100,top=100";
    
    var w = window.open(purl, winname, wndAttr);
    w.focus();
    }
    <a xhref="javascript:openPopup('one','popup1.html')" mce_href="javascript:openPopup('one','popup1.html')">Test Popup 1</a>
    <a xhref="javascript:openPopup('two','popup2.html')" mce_href="javascript:openPopup('two','popup2.html')">Test Popup 2<a>
  7. Hey Kumar,
    Thanx for the IE7 solution! I have been looking for a work around for more than a day now but couldn’t get it done. Thanks for the nice (and o so easy looking) script!

  8. I found another solution and I think this one is the complete one, if U want to close a window and open another in fullscreen mode without the “Bill’s” pop-up message in IE7 … 😉
    So:


    var t = (new Date()).getSeconds().toString()+''+(new Date()).getMinutes().toString()+''+(new Date()).getHours().toString();
    var etc="channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=1,status=1,toolbar=0"
    window.open("about:blank", t, etc );
    window.open('','_self',t,etc);
    document.forms[0].target=t;
    document.forms[0].submit();
    window.opener=null;
    window.close();
    window.close();

  9. Pingback: replica6a
  10. Hey Nikkkro,

    Thanks for that. However I got the following error in FF 2.0 :

    Error: document.forms[0] has no properties

    Regards,
    AJ

  11. 1.FOR [AJ] & [venu] -> u’r wright…it might not work in firefox…but my solution was exclusiv for IE7…cose I used it for a task in an web-based application running under IE6 or IE7 platform…so I wasn’t confrunted with such a problem 😉 …but u’r free to carry on …and solve this problem

    2.FOR [AJ] about the …it depends on how many forms do u have in u’r page…and to what form do u refer to….in the script…(forms[0] or forms[1] etc.)

    3.FOR [zaheerabbas] for me, it worked every time since I used that…so review u’r code and see if u wrote it with no mistakes.. 😉

  12. function closewindow() {
    window.open(?,?_parent?,?);
    window.close();
    }

    this works fine for IE 7.0 and for firefox 1.5 but not working for firefox2.0 version onwards… any suggestions appreciated.. thnx

  13. Hi,

    function closeTop()
    {
    var top = window.open(“”, “_top”);
    //top.close();
    top.focus();
    top.opener = top;
    top.close();

    }

    this works in IE6/IE7 for nested frames used in the program. Can any one suggest me to close the window without popup in FF2.

    Thanks & Regards,
    Satish R

  14. Pingback: Jessie
  15. Hi, im Mark and I guess im introducing myself!!

    I came across this site using ‘stumble’ (Fierfox) and thought alkl the ideas floating arounnd hree seem reaally interesting! (Although I don’t seem to agree with everything lol)

    Well, thats me!

  16. How about this method? Test upper operated without problem.
    (Test consisted in FF, IE6, IE7.)
    ———————————————————————————

    window.open(‘pop.html’,”,”); // Second Popup

    function tricky_win_close() {
    top.window.opener = top;
    top.window.open(”,’_parent’,”);
    top.window.close();
    }
    tricky_win_close();

  17. I am working on a webpage with multiple frames. None of the solutions suggested above works for FF2.0.0.6. Please let me know anybody who found a solution.

    Thanks

Leave a Reply to purwanto Cancel reply

Your email address will not be published. Required fields are marked *