Detect & trigger action while user exit webpage
Some of website developers are need to track on visitor’s activity during browsing their website. Even would like to track when the visitor exit or quit their website.
Below is a simple example on how to using a javascript command to track on visitor exit/close page action.
Example:
<script language=”javascript”>
function trackUser(){
//doing something here for your tracking.
}
window.onbeforeunload = function(){ trackUser(); }
</script>
12 Comments to “Detect & trigger action while user exit webpage”
Leave a Reply
onbeforeunload event fires even in any button click of a page..
so its pointless…..
Hi Nirasha,
Thanks for your comments on this.
But the onbeforeunload command will be only fire while visitor try to leaving/exiting the page.
The script is working fine on my side.
Might to share your opinion on this?
blfbyjnlzrohpxcxwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch 😉
What he’s trying to say, even if he is being a bit snide, is that if you click on the navigation within the website to go to a separate page from where you are currently (i.e. the user lands on the homepage and then navigates to About), this triggers the event, which you don’t want.
I’ve created a jQuery plugin called onUserExit that allows you to specify a function to run upon leaving the webiste (closing the site, or when leaving to an outside URL).
The way this is achieved is that upon the page completing load, I actually add click methods to all internal links within the page that execute a function called “userMovingWithinSite()”, which sets a boolean variable called “movingWithinSite” to true. So now, when the user clicks a navigation link or any link on the website WITHIN the website and the onbeforeunload event triggers, I can check to see if the user is actually leaving the site, or just navigating within it. If they are indeed leaving, then we execute your function!
Regards,
Scott Brooks
UserFirst Interactive
Hi Scott,
Thanks for your explanation & the solution on the user exit page.
Its great!!
Cheer~~!
My pleasure! Feel free to head on over to my site to check out some of my other jQuery plugins. I’ve just started my site this month and would love some feedback on the plugins I’ve built.
Regards,
-s
Great!
Scenario:
I have a web page where swf is embedded (it is a game site) where user can play using up, down ,right, left arrows.
Once a user starts his game and leaves the page as it is and browse a new page or he goes to “mycomputer” to view his folder. If the user comes to the game page the keys are not active, the user is not able to continue his game, until he/she clicks the swf.
So I want to track the event using javascript where the user leaves his page standstill.
I tried window.onblur it is working for plain text, but not with the swf embedded.
Problem persists only in IE browser.
Please suggest..
Thanks in advance
I want to display an alert when user closes the web browser. onLoad , onBeforeUnload are get triggered when user refresh the page or try to go to another page. What I really want to track is user closing the window by pressing (X) or Cntrl+W. I’m using JSF and XHTML and it’s a SEAM environment. Should work for FF and IE. Do you have any idea?
If you refresh a page, the event fires. It is useless.
ray,
You may want to add in some validation inside the function.
E.g:
if(document.location != ‘[YOUR SITE DOMAIN]’){
//PROCEED YOUR CODE
}
Browser close and page refresh giving the same domain name so its not working.
Hi Scott,
Thanks for your explanation & the solution on the user exit page.
Its great!!