Display alert on browser back button click? RRS feed

  • Question

  • Hi All,

    I want to display an alert once user clicks back button on browser. Here is the code i am using. It is working fine, but it is firing for all events in the page. Please correct me where i am doing wrong?

    I can able to see alert box after clicking back button in browser, but my problem is if i click any other button in my page also alert box is showing?

    1. window.onbeforeunload = function(evt)   
    2.     {   
    3.     if (typeof evt == 'undefined')    
    4.     evt = window.event;    
    7.         if(evt)   
    8.         {   
    10.         return "If you leave this page, your information will not be updated.";   
    11.                   }   



    Saturday, September 5, 2009 12:13 PM

All replies

  • There doesn't appear to be a documented property to determin the trigger of the unload event:


    However, as a bit of a hack. One possible solution would be to test the mouse coordinates of the clientX and clientY values are within the page. If they are within the page the user has not clicked any browser window buttons to fire the event.

    // Supplied by - KVK Consultancy (2009) http://www.kvkconsultancy.co.uk
    window.onbeforeunload = onBack;
    function onBack(evt)
    	if (evt == undefined)
    		evt = window.event;  
    	if (	(evt.clientX < 0) || 
    		(evt.clientY < 0) || 
    		(evt.clientX > document.body.clientWidth) ||
    		(evt.clientY > document.body.clientHeight)
    		alert('Unload from browser button press');
    		return "You clicked some browser button? Do you want to move away from this page?";
    	return undefined;
    Kind Regards,

    Provided by : http://www.kvkconsultancy.co.uk

    Tuesday, October 6, 2009 3:04 PM