0

I want to clear session value or remove session value when a user leaves a particular page. For that I had written this condition, it works in all browsers except Safari.

$(window).on("beforeunload", function()
{
   $.session.remove('visited');
});

Is there an alternative in Safari?

Arjun
  • 85
  • 1
  • 1
  • 10
  • Why aren't you using `onunload` event handler which is non intrusive. – Dipen Shah Aug 20 '19 at 15:14
  • 1
    Possible duplicate of [Is there any way to use window.onbeforeunload on Mobile Safari for iOS devices?](https://stackoverflow.com/questions/4127621/is-there-any-way-to-use-window-onbeforeunload-on-mobile-safari-for-ios-devices) – deblocker Aug 26 '19 at 12:18
  • 1
    @Arjun, do you know what error you might be receiving when you try your code in Safari? I am curious whether the "beforeunload" event is failing, or if it is the "window.sessionStorage" call that is not supported, or if it is something else. – Jonathan Walton Aug 26 '19 at 21:49

2 Answers2

2

beforeunload is for javascript window object , not for jquery $window object.

window.addEventListener('beforeunload', function (e) {
  // the absence of a returnValue property on the event will guarantee the browser unload happens
 console.log(e);
}); 
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Mayur Baldha
  • 126
  • 1
  • 15
  • I added window.addEventListener, here the issue is, it works fine for all browsers except for safari. In safari it keeps on loading continuously. – Arjun Sep 09 '19 at 10:58
  • I suggest use New browser events https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/1.5/Using_Firefox_1.5_caching#New_browser_events – Mayur Baldha Sep 10 '19 at 06:24
1

Can you refer the following links, it may be help you to understand.

https://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/

Load/Unload Event Handlers :

Web developers can make use of the load and unload events to do work at certain points in the lifetime of a web page.

The purpose of the load event is quite straightforward: To perform initial setup of a new page once it has loaded.

The unload event is comparatively mysterious. Whenever the user leaves a page it is "unloaded" and scripts can do some final cleanup.

The mysterious part is that "leaving the page" can mean one of a few things:

  1. The user closes the browser tab or window, resulting in the destruction of the visible page.
  2. The browser navigates from the old page to a new page, resulting in the destruction of the old visible page.

The Page Cache makes this even more interesting by adding a new navigation possibility:

  1. The browser navigates from the old page to a new page, but the old visible page is suspended, hidden, and placed in the Page Cache.

Some existing stack answers also available for this :

window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

John Peter
  • 2,870
  • 3
  • 27
  • 46