Closing a browser window from a javascript link

Posted on 2013-01-23
Last Modified: 2013-01-28
I am trying to set up a link to close a browser window using: onClick="javascript:window.close();

The window is initially opened from the main page and displays a signup form. When the form is sent a confirmation page appears and it is this page that has the button to close the window using the javascript function above.

The javascript works if I open the window directly to display the confirmation page but it is not working when the confirmation page is called from the original page with the form.

Could someone point me in the right direction please.
Question by:grigorovsky
  • 4
  • 3
LVL 22

Expert Comment

ID: 38810338
Can you post your code? This can help us to resolve your problem quickly.

Expert Comment

ID: 38810363
Use self.close()

<a href="" onclick="self.close();return false;";>Close This window</a>

Author Comment

ID: 38810852
Self.close() doesn't work either but I have now discovered that parent.close() does.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Expert Comment

ID: 38810861
hmm.  I thought you were wanting the button on the confirmation window to close itself, not the main form window.

Author Comment

ID: 38810867
This works:

<a href="#" class="button" onClick="javascript:parent.close();">Close Window</a>

These don't:

<a href="#" class="button" onClick="javascript:self.close();">Close Window</a>
<a href="#" class="button" onClick="javascript:window.close();">Close Window</a>

Accepted Solution

grigorovsky earned 0 total points
ID: 38810891
The confirmation page appears in the same window as the sign up form and has the close button at the bottom. Apologies if that wasn't clear from my original post.

I originally had the confirmation page script set to window.close() and this didn't work. It appears that parent.close() is the correct method in this situation.

Expert Comment

ID: 38810985
Yes, that would be correct in your situation.  Sorry for the misunderstanding.

Author Closing Comment

ID: 38826195
My own solution was the correct one in this situation.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question