Jump to content

How Do I Catch The Page Leave Event?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Like if the user is leaving the page (such as typing another URL in the address bar), or closing the tab or window, is it possible to catch that event and do something about it?

It's just I have this JavaScript that contacts the server every once in a while and tells it what page it's on, for statistical purposes. But if the user types in another URL in the address bar, or closes the tab or window, then the user would still be marked as "viewing the page," until that once in a while passes.

So it would have been nice if I could make the script catch that event and tell the server that it's not on that page anymore.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
<html>
    <head>
        <script type="text/javascript">
            function deny_unload()
            {
                alert("You shall not pass!");
            }
        </script>
        
    </head>
    <body onbeforeunload="deny_unload()">
        <a href="javascript:window.close()">Close Me</a>
    </body>
</html>

This will always close unless you put "return deny_unload()", in which case you should return a boolean indicating whether the operation should proceed or not.
sudo rm -rf /

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Is there a way to set that in JavaScript?

Because it's not that easy for me to change the BODY tag via HTML, because there are multiple pages. But the script I'm working on is included in all the necessary pages.




EDIT: I defined a function that I called win_unload, and I tried different things:
// this: 

document.body.onBeforeUnload= 'return win_unload();'; 

// and this: 

document.body.onBeforeUnload= win_unload; 

// and this: 

document.body.onBeforeUnload= `win_unload`; 

// and this: 

window.onBeforeUnload= 'return win_unload();'; 

// and this: 

window.onBeforeUnload= win_unload; 
And for some reason none of these seem to start the win_unload() function when I click a link on the page, type another URL in the address bar, or close the tab (in Firefox).

And the win_unload() function definition:
			function win_unload(){ 

				alert("Click OK to continue. "); 

				return true; 

			} 


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
<html>
    <head>
        <script type="text/javascript">
            function unload_failed()
            {
                alert("You shall not pass!");
            }
            
            function unload_ok()
            {
                alert("Fine, go ahead.");
            }
            
            function change_handler()
            {
                alert("From '" + window.onbeforeunload + "'");
                window.onbeforeunload = unload_ok;
                alert("Changed to '" + window.onbeforeunload + "'");
            }
        </script>
        
    </head>
    <body onbeforeunload="unload_failed()">
        <a href="javascript:window.close()">Close Me</a><br/>
        <a href="javascript:change_handler()">Change</a>
    </body>
</html>


This worked for me.
sudo rm -rf /

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Use the document.onbeforeunload event handler.

EDIT: I see Dargueta beat me to that answer. Sorry I couldn't provide any new advice.
Programming is a journey, not a destination.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I'm pretty sure events are Microsoft-specific.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users