javascript to detect signed-in user leaving webpage or website

  • Tình trạng: Closed
  • Giải thưởng: $10
  • Các bài thi đã nhận: 7

Tóm tắt cuộc thi

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

Các kĩ năng yêu cầu

Những bài dự thi tốt nhất dự cuộc thi này

Xem thêm bài dự thi

Bảng thông báo công khai

  • adrianjchen
    adrianjchen
    • cách đây 5 năm

    Why is submission in JPG, PNG, GIF format?

    • cách đây 5 năm
  • coneldllc
    Chủ cuộc thi
    • cách đây 5 năm

    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.

    • cách đây 5 năm
    1. aaronsanders
      aaronsanders
      • cách đây 5 năm

      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.

      • cách đây 5 năm
    2. coneldllc
      Chủ cuộc thi
      • cách đây 5 năm

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

      • cách đây 5 năm
  • aaronsanders
    aaronsanders
    • cách đây 5 năm

    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.

    • cách đây 5 năm
    1. aaronsanders
      aaronsanders
      • cách đây 5 năm

      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.

      • cách đây 5 năm
    2. coneldllc
      Chủ cuộc thi
      • cách đây 5 năm

      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.

      • cách đây 5 năm
  • venisoftvenix
    venisoftvenix
    • cách đây 5 năm

    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.

    • cách đây 5 năm
  • coneldllc
    Chủ cuộc thi
    • cách đây 5 năm

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

    • cách đây 5 năm
  • afsana1313
    afsana1313
    • cách đây 5 năm

    did you mean closing the browser or logging out?

    • cách đây 5 năm

Xem thêm bình luận

Làm thế nào để bắt đầu với cuộc thi

  • Đăng cuộc thi của bạn

    Đăng cuộc thi của bạn Nhanh chóng và dễ dàng

  • Nhận được vô số bài dự thi

    Nhận được vô số Bài dự thi Từ khắp nơi trên thế giới

  • Trao giải cho bài thi xuất sắc nhất

    Trao giải cho bài thi xuất sắc nhất Download File - Đơn giản!

Đăng cuộc thi ngay hoặc tham gia với chúng tôi ngay hôm nay!