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”

  1. Nirasha 19 November 2008 at 9:03 pm #

    onbeforeunload event fires even in any button click of a page..

    so its pointless…..

  2. h2Guru 20 November 2008 at 10:25 am #

    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?

  3. Acconabaiva 28 December 2008 at 2:14 am #

    blfbyjnlzrohpxcxwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

  4. Scott Brooks 28 December 2008 at 7:15 pm #

    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

  5. h2Guru 28 December 2008 at 9:27 pm #

    Hi Scott,
    Thanks for your explanation & the solution on the user exit page.
    Its great!!
    Cheer~~!

  6. Scott Brooks 30 December 2008 at 7:04 pm #

    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

  7. HTMLDesigner 5 February 2009 at 9:34 pm #

    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

  8. Sanjeewa 10 November 2009 at 1:28 pm #

    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?

  9. ray 25 June 2010 at 11:41 am #

    If you refresh a page, the event fires. It is useless.

  10. h2Guru 25 June 2010 at 1:41 pm #

    ray,

    You may want to add in some validation inside the function.
    E.g:

    if(document.location != ‘[YOUR SITE DOMAIN]‘){
    //PROCEED YOUR CODE
    }

  11. Nilesh 9 February 2012 at 12:51 pm #

    Browser close and page refresh giving the same domain name so its not working.

  12. George 11 March 2013 at 1:31 am #

    Hi Scott,
    Thanks for your explanation & the solution on the user exit page.
    Its great!!


Leave a Reply