User First Marketing Blog

User First Development Blog

Development vs. Marketing Blog

client login
  • Tobii Eye Tracking
  • Web Analytics 2.0
  • UI / UX Development

onUserExit - Execute Code When a User Leaves or Closes Your Website

onUserExit - Execute Code When a User Leaves or Closes Your WebsiteonUserExit is a jQuery plugin allows you to easily wire a function to execute when the user leaves your website. Internal website pages are filtered out, allowing the function to ONLY trigger when the window is closed or the user navigates to an external website.

I’m sure many of you have read about the javascript function onbeforeunload() or in jQuery $(window).unload() which triggers when a user leaves a page. Well the trouble is if the user clicks on the navigation within your website to go to a separate page from where you are currently (i.e. the user lands on the Home and then navigates to About), this triggers the event, which in this case you don’t want.

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!

See the full demo posted in our creations area.

Download Plugin
Version 1.0 - onUserExit js files + dependencies

Download the complete demo here.

The full project can also be found on the jQuery website in the plugins directory under onUserExit.

Please feel free to submit suggestions for changes to this plugin!

5 Responses to “onUserExit - Execute Code When a User Leaves or Closes Your Website”

  1. Hi! Your article rocks and can be a great understand!…

  2. Sisal rug says:

    I discovered your blog on google and verify a couple of of the early posts. Retain up the great perform. I just added up your RSS feed to my MSN News Reader. Searching forward to reading through a lot more from you later on!…

  3. Quite regularly I go to this weblog. It incredibly a great deal is pleasant to me. Thanks the author!…

  4. Me and my friend were arguing about an matter similar to this! Now I realize that I had been proper. lol! Thanks with the data you write-up….

  5. Hi, thank you so very much for these tips!…

Leave a Reply