Using script to close a window, etc….

In a previous post, I provided JavaScript to open a page in a new window. It may sound funny, but once the window is opened, what does the visitor do when they're done with it?  Generally, they can click on the red X in the upper right corner to close the window, but I've seen a lot of sites, and likely you have too, that provide a button that visitors can click on to close the window.  This code is really easy, so today's tip is really short, but here goes.

The code to add a button that closes the current window is shown below.

<button onclick="javascript:window.close()">Close</button>

Alternatively, if you want to do this with text, you can use the A element and place the JavaScript code in the href attribute, as shown in the following code.

<a href="javascript:window.close()">Close</a>

There is one thing that you should know about this code.  If you use the close method on a window that was not opened using script, Internet Explorer displays a message asking visitors if they want to close the window.  In Netscape Navigator and Mozilla, the code won't work at all, and the JavaScript console displays the message "Scripts may not close windows that were not opened by script."  So this example works best in a page that you display in a popup window using script code similar to the code I provided in my earlier post, open a page in a new window, mentioned above.

That's it.  No magic here; nothing terribly complicated. Just simple code to close the active window. To use this code, just copy it into your page as is.  When you display the page in the browser, you will see a button.  Just remember that depending on the browser you use, it may not function or may not function as you want it to.

Well, that's pretty much my post for the week.  It's been busy.  I've been reworking the FrontPage Server Extension pages on MSDN.  Okay, so this has been in the works for some time, but we've had a few glitches.  The glitches seem to be smoothed, so if all works out well, we should have updated content available within about four weeks.

I'm reviewing all the feedback comments that people have provided as I update each page. That said, I want to give a plug for everyone disposed to rating MSDN articles.  I don't know how other teams view this data, but in Office, we take customer comments for articles very seriously.  Unfortunately, we can't always do anything about them.

For example, if someone rates an article as a 1, the lowest score possible, and provides a comment like, "This sucks!" or something negative about Microsoft, well, I hate to say it, but I can't do anything with a comment like that.  So if you often rate, or even if you don't but would consider doing it if you knew that someone actually looks at your comments, please put in comments that help us to know how to revise the content.  Were the instructions bad?  Where?  Which ones? Was something unclear? What?  Be as specific as you can without writing a book.

Oh, and one more thing, although I have on occasion been known to respond to people who provide email addresses in article comments, I don't make any promises that the response will either happen or will happen quickly, so if you need assistance with an article and need a response back, the best way to do this is to send us an email.  You can do this from our Feedback page.  Just click Talk to Us, and an email window opens where you can write to us.  I receive every one of those messages, and although I don't encourage it as a support mechanism, if you've exhausted all of your other options or need specific help with an article, I want to hear from you.

Alternatively, feel free to send me a message through my blog.  Again, I respond to every message, and if someone provides an email address in a blog comment, I will often contact them directly as well as through the blog.

Have a great weekend.

Comments (23)
  1. matt says:

    You’re probably aware of this, but you can force a window to close without prompting in IE using something like the following:

    <a href="javascript:close(opener=0)">Close</a>

    But again, it doesn’t work in Netscape and Mozilla.

  2. Gabriel says:

    yeah, Matt´s right.

    you go:



    and off u go, u can change any window you want.

  3. Gabriel says:

    you can CLOSE any window you want, that is.

  4. alxjvr says:

    in firefox, you need to turn on the "pref("dom.allow_scripts_to_close_windows", false);" option in (normally) C:Program FilesMozilla Firefoxgreprefsall.js

  5. I’ve seen how to open a new window to a certain size, but, how about when you want to open/maximize the window. Can you help?



  6. Lisa Wollin says:

    It is possible, with just a little bit of work. The following code takes the available height and width of the browser (which is the height and width of the screen minus the room taken up by toolbars, scrollbars, etc.) and resizes the window to these measurements. Then it moves the browser window to the top left corner of the screen.

    function changeBrowserSizeOnLoad()


    var sHeight = screen.availHeight;

    var sWidth = screen.availWidth;

    window.resizeTo(sWidth, sHeight);

    window.moveTo(0, 0);


    All you need to do is add an onload item to your opening body tag, like so:

    <body onload="javascript:changeBrowserSizeOnLoad();">

    I’ve tested this in Internet Explorer, Netscape Navigator, and Mozilla, and it works in all three exactly as expected.

    Hope this helps,


  7. Bob Seitz says:


    Now how can I modify the applet for a hover button to close a window opened separately so the "Home" button doesn’t create a copy of the originating "home" page in the new window?


  8. charliehower says:

    Good,But how can I modify the browser window look when the page is loaded?

    e.g. I want the scrollbar,dicrionaries bar and file bar disappear all as soon as I load a page .Can I do it using js?

    p.s. Not using function.

  9. Brian says:

    do‘theurl’, ‘fullscreen’);

  10. rakesh halder says:

    The first step is to fool the browser into thinking that it was opened with a script…”,’_parent’,”);

    This opens a new page, (non-existent), into a target frame/window, (_parent which of course is the window in which the script is executed, so replacing itself), and defines parameters such as window size etc, (in this case none are defined as none are needed). Now that the browser thinks a script opened a page we can quickly close it in the standard way


    And there you have it – I told you it was simple! In case you didn’t follow that, here is the complete solution in two easy steps:

    1. write the following code to the head of your page…

    <script language="javascript" type="text/javascript">

    function closeWindow() {”,’_parent’,”);




    2. Set your link like this:

    <a href="javascript:closeWindow();">Close Window</a>

    And there you have it – A problem that it seemed, (according to my trying to find a solution before coming up with this one anyway), to have baffled everyone else.

  11. Varun Pant says:

    It was great. The issue was solved satisfactorily.

  12. Syx says:

    @rakesh halder: THANK YOU!

  13. thank you rakesh_halder

  14. KEN says:

    Dear programmers,

    javascript window closer codes does’t not work on Internet Explorer 7. How can i work it in IE 7. Pls help me…

    Thank you

  15. Nina says:

    Hi there,

    I have the following code used to close a window, but once the user clicks ‘Close’, I need to customize the confirmation message that appears. Is there a way to control this in the same line item?

    QUIT_CONTROL=<a href="javascript: window.close();"><img src="%QXQ_GRAPHICS_DIR%exit.jpg" width="98" height="26" border="0"></a>

    Thanks in advance for the help!

  16. Nina says:

    Hi there,

    I have the following code used to close a window, but once the user clicks ‘Close’, I need to customize the confirmation message that appears. Is there a way to control this in the same line item?

    QUIT_CONTROL=<a href="javascript: window.close();"><img src="%QXQ_GRAPHICS_DIR%exit.jpg" width="98" height="26" border="0"></a>

    Thanks in advance for the help!

  17. yalcinKENan says:

    Yes i show you a code for your customize;

    <a onclick="javascript:return confirm(‘This page will close.nnAre you sure?’)" href="javascript:window.close()"><img src="%QXQ_GRAPHICS_DIR%exit.jpg" width="98" height="26" border="0"></a>


  18. Dawod says:

    i want to close window in the Mozilla Firbox using the Javascript.

  19. anand says:

    the above samples to close window easily were really helpful. thx,

    i needed to close either  IE or firefox so I just did as below, and it works good.

    <a href="javascript:close(opener=0);”,’_parent’,”);window.close();">Click here</a>

  20. BettyW says:

    FYI, I used anand’s code successfully for Firefox and IE, but Netscape isn’t happy with it  – button doesn’t close the window.

  21. DavidG says:

    I am using IE7 RC1 but my window.close() always displays the confirmation box even when I open the window using script (i.e.

    Is this a bug or does anybody know how to get around this?


  22. Darkman-CV says:

    In my IE7 Beta 3 work solution:

    <script language="javascript" type="text/javascript">

     function closeWindow()





  23. Dr.Sizzle says:

    I tried every one of these scripts and none seem to close the window.

    I am opening a new window from flash. If I use the window.close90; or onClick=self.close90; as soon as the window opens it works fine. But here its the thing. The pop-up window has links inside it’s self. like page 1,2, & 3. If you click on and of those links the close button does not work. I’m assuming this is the not opened but javascript not close by javascript. Maybe it’s the Mozilla thing. All I know is I have multiple pages that need to close independently at and time.

    rakesh halder’s method loads nothing into the page. so I neve seen a thing that way.

    Any clue on how to make a close button that will work regaurdless? or better yet maybe the answer is in

    opening these links using javascript with in the parent page?



Comments are closed.

Skip to main content