View Full Version : Display alert when close the browser window

Dec 24th, 2009, 05:14 AM
I need to display alert when I close the browser window. Now I am using the following code to display the alert. But the alert is getting displayed even I click on any links. But the alert should not displayed when i click the link.

If you have any solution, please let me know.

Thanks in advance.

This is my code:




function closeIt()


return "";


window.onbeforeunload = closeIt;




<a href="http://www.test.com">Click here to navigate to




Philip M
Dec 24th, 2009, 07:58 AM
Try this:-

<script type = "text/javascript">
var ClosingVar = true;
var message = "This is the bespoke message";
window.onbeforeunload = ExitCheck;
function ExitCheck() {
if (ClosingVar == true) {
ExitCheck = false
return message;

<a href="http://www.mysite.com" onClick="ClosingVar=false">Go to another page</a>

Remember that the window.onbeforeunload event is fired every time the page is about to unload. This means that if the user clicks "refresh" or a button on your page, and your page posts back to the server, the window.onbeforeunload event will fire.

This sort of prompt tends to annoy the user, and should be avoided unless it really adds value.

Please read the forum rules and posting guidelines. And remove unnecessary white space.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.