Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript question: Onbeforeunload or Onunload?

Tags:

So I want to store some information in localstorage of a browser when the page is refreshed or the user exits the page for future use. I figured that I'd use some Javascript to detect the event and store the state of the page visit.

Now my predicament is: shall i use Onbeforeunload or Onunload method to accomplish this objective?

Thanks

like image 444
pratyk Avatar asked Sep 23 '10 05:09

pratyk


People also ask

What is the difference between Onbeforeunload and Onunload?

One significant difference (other than cancelability) between the onbeforeunload and onunload is that the former is triggered for download links and the latter is not.

What does Onunload do in JavaScript?

The onunload attribute fires once a page has unloaded (or the browser window has been closed). onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.)

How do I use Onbeforeunload in JavaScript?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.

What triggers Onbeforeunload?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.


2 Answers

Why not register it with both just to be on the safe side? Just have the listener do a check to see if the data's stored yet and exit early.

Here's a quick example using event properties:

window.onunload = window.onbeforeunload = (function(){

  var didMyThingYet=false;

  return function(){
    if (didMyThingYet) return;
    didMyThingYet=true;
    // do your thing here...
  }

}());

Or you could use attachEvent:

(function(){

  var didMyThingYet=false;

  function listener (){
    if (didMyThingYet) return;
    didMyThingYet=true;
    // do your thing here...
  }

  window.attachEvent("onbeforeunload", listener);
  window.attachEvent("onunload", listener);    

}());
like image 75
Dagg Nabbit Avatar answered Oct 12 '22 02:10

Dagg Nabbit


The proper place to do it is onunload. onbeforenload is meant to be used when you may need to stop the user from leaving the page, like when there is unsaved data.

I would be weary of writing code to cover all cases because you're not willing to test all cases. If you do your research and find that's it's too complicated across browsers, yeah, go ahead and do as much as you can. However, I believe there is no problem with doing local storage onunload. I actually did run into a problem, trying to send information to the server onunload. That did not happen reliably across browsers so I did end up with an approach that used both unload and beforeunload, but only after due diligence.

So my suggestion is do it onunload, check all your browsers.

like image 38
Juan Mendes Avatar answered Oct 12 '22 00:10

Juan Mendes