Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have to give user message to close browser.
I have used following script

<script type="text/javascript">
<!--
var g_isPostBack = false;

window.onbeforeunload = function ()
{
 if ( g_isPostBack == true )
  return;

 var closeMessage =
  'You are exiting this page.\n' +
  'If you have made changes without saving, your changes will be lost.\n' +
  'Are you sure that you want to exit?';

 if ( window.event )
 {
  // IE only...

  window.event.returnValue = closeMessage;
 }
 else
 {
  // Other browsers...

  return closeMessage;
 }

 g_isPostBack = false;
}

// -->
</script>



It is working fine BUT whenever I have click on any server control like button then it giving user message.

I dont want user message on clicking Button

Please help me

Thanks.
Posted
Comments
CRDave1988 7-Mar-12 2:36am    
Can U make your statement more clear "I have to give user message to close browser"
Caleb McElrath 8-Mar-12 0:40am    
I suggest passing a variable to the function like: function (e) { ...event code... } then you can add this: e = e || window.event; It almost guarantees a workable event object. That said, this only works with browsers other than IE: examine the event object to obtain "event.srcElement.activeElement". there you can get the element type such as a link (which would indicate it is a page). Knowing this, you will then be able to cancel the onbeforeunload event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900