javascript to detect signed-in user leaving webpage or website

  • Κατάσταση: Closed
  • Βραβείο: $10
  • Ληφθείσες Συμμετοχές: 7

Σύνοψη Διαγωνισμού

For a user that has signed in, we need to let the server know when this user has left the website.

For example, it can be some JS code written which will detect a user leaving a webpage via navigation buttons(forward,backward), exiting browser, closing tab etc. It must work with all recent browsers (FF, Chrome, IE, etc) without prompting any kind of dialog box.

our focus for this is not on mobile. it's mainly for usage on computers. btw, Ideally, we need only one firing of AJAX! What we basically need is: for this whole session when the user is using our website, it doesn't matter how many pages he has browsed or opened, when the user finally leaves, we want this one time fire. so, you can also see if the session is finished, and then trigger this. so, if there's a way to detect the ending of session at either user or server side, then that's what we need.

The server should receive a trigger event once this happens. A sample code is attached. But it's not working on all browsers.

The following codes and links might be helpful for you!

JS: How to detect when a user leaves your site excluding reload
https://stackoverflow.com
$(window).bind('beforeunload', function() {
// ajax call perhaps
// triggering a write to db or filesystem...
});
this might be helpful?
or this?
<html>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.somewhere.com">Click here to navigate to
www.somewhere.com</a>
</body>
</html>
// body
<body onunload="goodbye()">

//window
window.onbeforeunload=function(e){
  var e = window.event||e;
  e.returnValue=("are you sure to leave the website?");
}
//body
<body onbeforeunload="goodbye()">
//window
window.onbeforeunload=function(e){
  var e = window.event||e;
  e.returnValue=("do you want to leave the website?");
}


Best way to detect when a user leaves a web page?
https://stackoverflow.com
this should be helpful


On browser close event
https://stackoverflow.com


Best way to detect when a user leaves a web page?
https://stackoverflow.com


how to detect when a user leaves your site - WebDeveloper.com Forums
https://www.webdeveloper.com


Can i detect if user is leaving my site?? - WebDeveloper.com Forums
https://www.webdeveloper.com

https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

Προτεινόμενες Δεξιότητες

Κορυφαίες καταχωρήσεις από αυτόν τον διαγωνισμό

Προβολή Περισσότερων Συμμετοχών

Δημόσιος Πίνακας Διευκρινίσεων

  • adrianjchen
    adrianjchen
    • Πριν από 5 χρόνια

    Why is submission in JPG, PNG, GIF format?

    • Πριν από 5 χρόνια
  • coneldllc
    Κάτοχος Διαγωνισμού
    • Πριν από 5 χρόνια

    our focus for this is not on mobile. it's mainly for usage on computers. btw, Ideally, we need only one firing of AJAX! What we basically need is: for this whole session when the user is using our website, it doesn't matter how many pages he has browsed or opened, when the user finally leaves, we want this one time fire. so, you can also see if the session is finished, and then trigger this. so, if there's a way to detect the ending of session at either user or server side, then that's what we need.

    • Πριν από 5 χρόνια
    1. aaronsanders
      aaronsanders
      • Πριν από 5 χρόνια

      Ahhh, okay. I can do something that will address that better. I was just trying to optimize making sure it fired based on the contest description; right now the solution I have in will fire every time the user closes (or for the first one, even browses away from) a tab. I'll try and submit a javascript-only and a backend solution to track a whole session soon, the backend will be more reliable but it's a little more heavy. My feeling is if it doesn't absolutely need to be 100% you'll prefer the javascript version for easy implementation.

      • Πριν από 5 χρόνια
    2. coneldllc
      Κάτοχος Διαγωνισμού
      • Πριν από 5 χρόνια

      no need to be 100%. if it fires a request every time a user closes a tab, it might be ok!

      • Πριν από 5 χρόνια
  • aaronsanders
    aaronsanders
    • Πριν από 5 χρόνια

    I put in the best javascript solution I have and it will work reliably for any browser (including mobile), I hope it solves your problem. However, if you want to be absolutely certain you track even, e.g., a user losing their internet connection it requires some server side work; you'd be having the user send a small amount of information every few seconds, and then closing their session if you don't get any data for a long enough time. It's the only 100% certain way to know when a user is gone and it's how, for example, Google tracks people's browsing behavior, but it requires more than just javascript.

    If you want something like that or anything else tweaked in my submission, let me know and I'll be happy to do it.

    • Πριν από 5 χρόνια
    1. aaronsanders
      aaronsanders
      • Πριν από 5 χρόνια

      Okay! As long as it doesn't have to be perfect javascript can do a pretty good job at it. My submission at the top of #4 is the best way I can find to track when a user closes or browses away from a page; it will fire as reliably as possible on as many browsers as possible and puts data into the $_GET array in the PHP script. If there's a compatibility problem (there shouldn't be, but you never know) or if you don't want to change your script too much the second block of code is a quick fix to your existing script to make it run synchronously, which should improve its reliability somewhat, maybe or maybe not good enough for what you're doing. It would still not fire on mobile very reliably, for example. Unfortunately most web browsers won't send UDP, or I would use that; it's all AJAX of some kind, the only real choices are how and under what conditions to send it.

      • Πριν από 5 χρόνια
    2. coneldllc
      Κάτοχος Διαγωνισμού
      • Πριν από 5 χρόνια

      no need to use the one I uploaded. it's just an example. it's 100% ok just to use your script. also, our focus for this is not on mobile. it's mainly for usage on computers. btw, here, when the user leaves all the pages of the website, you fire AJAX, or when a user opens multiple tabs of the website and close each tab, it'll fire one AJAX? Ideally, we need only one firing! What we basically need is: for this whole session when the user is using our website, it doesn't matter how many pages he has browsed or opened, when the user finally leaves, we want this one time fire.

      • Πριν από 5 χρόνια
  • venisoftvenix
    venisoftvenix
    • Πριν από 5 χρόνια

    window.onbeforeunload is not the only thing to solve this issue.

    Users can open multiple tabs of your application, and we need to identify if all the tabs are closed or not. But identifying a numbe r of tabs is not possible, so we need to restrict the opening of tabs to one by using cookies.
    Also, users can refresh the page, if so the same window.onbeforeunload gets called before unloading of an existing page and get new refreshed content. So need to handle that too.

    Once if all above precautions are taken and on restricting to single window/tab usage, if the user closes the browser, the final sign-off backend service has to call using an AJAX function.

    • Πριν από 5 χρόνια
  • coneldllc
    Κάτοχος Διαγωνισμού
    • Πριν από 5 χρόνια

    for only a user that has logged in, if the user leaves the site, for example, close the browser, log out, etc.

    • Πριν από 5 χρόνια
  • afsana1313
    afsana1313
    • Πριν από 5 χρόνια

    did you mean closing the browser or logging out?

    • Πριν από 5 χρόνια

Προβολή περισσότερων σχολίων

Πώς να ξεκινήσετε με τους διαγωνισμούς

  • Δημοσιεύστε τον διαγωνισμό σας

    Αναρτήστε τον διαγωνισμό σας Γρήγορα και εύκολα

  • Λάβε ένα σωρό συμμετοχές

    Λάβετε Πολλές Συμμετοχές Από όλο τον κόσμο

  • Βραβεύστε την καλύτερη καταχώρηση

    Βραβεύστε την καλύτερη καταχώρηση Κατεβάστε τα αρχεία - Εύκολα!

Αναρτήστε ένα Διαγωνισμό Τώρα ή Ελάτε Μαζί Μας Σήμερα!