Load and unload events don't always fire

When you navigate with the Back and Forward buttons, Opera doesn't fire any load and unload events.

Test page. Workaround is not included.
Reported by ppk.

Opera | Reported on 26 November 2004.

This site is no longer maintained. I’m sorry, but it’s just too much work for too little return. You can continue to browse old bug reports, though.

Search reports by browser:

Atom RSS


(Add your own)

1 Posted by Chris Hester on 26 November 2004 | Permalink

This is because Opera fetches previous pages from its cache. So nothing is reloaded. This is what makes it super fast.

2 Posted by Dante Evans on 26 November 2004 | Permalink

Chris is absolutely right. Opera's speed and super-cool features are what makes me love it. This bug can be defeated by simply going to File: Preferences and unchecking "Cache documents"

3 Posted by ppk on 26 November 2004 | Permalink

Unfortunately that turns out not to be the case. When I unchecked Preferences -> History and Cache -> Cache documents and did the test again the load and unload events still didn't fire when I used the Back and Forward buttons.

4 Posted by Dante Evans on 27 November 2004 | Permalink

My mistake, it was something else I was thinking of.

5 Posted by Tobias Michaelsen on 27 November 2004 | Permalink

The page is indeed loaded. It is just loaded from the cache and is not fetched from the server.
The event should be fired when the document is loaded into the DOM.

6 Posted by Wout van der Klip on 27 November 2004 | Permalink

Strange...when I use the back and forth buttons, the load event always fires. The unload event doesn't. (Opera 7.23)

7 Posted by Thomas Goyne on 28 November 2004 | Permalink

The event is triggered when the page is loaded into the DOM. However, using back and forward does not cause the page to be reloaded in Opera as it does in other browsers. There is little difference between switching between pages in different windows/tabs and switching between pages in the same window/tab (with back/forward) in Opera, and surely you would agree onload/onunload shouldn't trigger when the user switches windows.

8 Posted by Thomas Goyne on 30 November 2004 | Permalink

Why should there be a difference? I've tweaked my gestures a bit so that if I try to go forward or back and there isn't anything to go forward or back to, it switches to the next/previous page. As I generally have several times more pages open than will fit on the pagebar, often the only indication that I'm switching between open pages and not going back/forward is that the back button is greyed out (and I could fix that if I cared to).

I'm not sure what the specs constitutes loading, but I'm not sure how you could possibly call what Opera does on back/forward loading. The cached html isn't reparsed, forms stay filled out, and changes to the page made with javascript remain. If anything, triggering onload when the page isn't being loaded would be a bug.

9 Posted by TarquinWJ on 1 December 2004 | Permalink

Correct. But for most pages, this does not matter, as Opera also remembers the runstate of any scripts that were running - so when you come back, the scripts continue running just like you never left. On virtually all sites, this means that there is no need for onload or onunload to fire. This only causes problems in framesets like this site - and personally, I am quite happy to live with it not highlighting the correct link in the navigation panel. This behaviour is a part of what makes opera so fast, and I wouldn't want it changed for anything.

10 Posted by ppk on 1 December 2004 | Permalink

Sorry, but to me this all sounds suspiciously like fans of a browser explaining away bugs.

It's simple, really:
1. Load and unload should fire whenever a user enters or leaves a page
2. Opera doesn't fire them in all cases.
3. Therefore Opera has a bug.
4. All explanations about speed and such don't change this fact.

11 Posted by TarquinWJ on 1 December 2004 | Permalink

absolutely it is fans explaining away bugs, because we don't consider them to be bugs. we want it to behave that way. I agree that web developers need to be aware of it, but they should also be aware that opera deals with it itself by also retaining script runstate

12 Posted by ppk on 1 December 2004 | Permalink

Well, at least that's honest.

Let's agree to disagree, then.

13 Posted by Thomas Goyne on 2 December 2004 | Permalink

1. Load and unload should fire whenever a user enters or leaves a page

I actually disagree with that statement. If that was true, the events should be named onenter and onleave. From the DOM2 standard: "The load event occurs when the DOM implementation finishes loading all content within a document, all frames within a FRAMESET, or an OBJECT element." Opera does trigger onload when the page is loaded. However, back/forward does not cause the page to be reloaded, so onload is not triggered again. If something somewhere said that the back button should force the page to be reloaded not doing so would be a bug, but thankfully no spec says that.

It does appear that Opera is violating the spec in not triggering unload, as "The unload event occurs when the DOM implementation removes a document from a window or frame." Based on the differences in wording, I'd say its fairly clear that when the DOM events spec was written it had not occured to anyone that a browser wouldn't nessesarily reload a document whenever it is put into a window, which is odd, as Opera's behavior in this predates DOM2.

In short, it goes against what was probably intended, but doesn't actually go against what is written.

14 Posted by Mikko Rantalainen on 20 October 2005 | Permalink

Firefox 1.5 will also include this feature. However, Firefox generates events "pageshow" and "pagehide" where "load" and "unload" would normally be generated. See http://developer.mozilla.org/en/docs/Using_Firefox_1.5_caching for more details.

15 Posted by David Bloom on 19 July 2006 | Permalink

Here's a workaround for onload:
<img src="javascript:location.href='javascript:yourFunction();';">

yourFunction will then be called every time the user returns to your page using the back/forward buttons. It only works if you "nest" the javascript: URL for some reason.

Note that if you plan on using CSS to hide this image, you can not set it to "visibility:hidden" or "display:none" because then Opera won't "load" it. Instead, use "position:absolute" and position it offscreen.

Demo page: http://bloomd.home.mchsi.com/opera-onload.html

16 Posted by Victor on 19 September 2006 | Permalink

The above work around is far from perfect on SSL sites because in Internet Explorer it prompts a warning that the page is trying to load "Non Secure Items"

17 Posted by David Bloom on 8 October 2006 | Permalink


The code can be hidden from IE user agents in this case. Only Opera has the bug so just make a script that adds the code to the document on Opera only.

18 Posted by Joel Jennings on 21 January 2007 | Permalink

Opera does not fire the onunload event when the user refreshes the page.

This is specifically wrong: you argued that the event was not triggered because of caching, but it's more pervasive than that.

19 Posted by wwwwoelferl on 29 March 2008 | Permalink

This is the solution:

history.navigationMode = 'compatible';

see http://www.opera.com/support/search/view/827/

20 Posted by Zach Leatherman on 18 April 2008 | Permalink

Are you sure that method works?

In my tests it still wasn't firing onunload using either of the fixes listed in that link:

history.navigationMode = 'compatible';

Opera 9.27 on Ubuntu Linux