Wilhelm Murdoch
Wilhelm Murdoch

Reputation: 1816

Opera: Detecting back, forward, refresh and close events

I'm currently working on a jQuery plugin that tracks a visitors mouse behavior. Movements, clicks, scrolling and resizing are all recorded and sent, via Ajax, to a location where this data is parsed and stored.

Originally, the data is sent to a script when the user leaves the page. By 'leaves' I'm referring to refreshing, going back and forth though history, closing the window/tab and going to a different address.

The solution works in all browsers EXCEPT for Opera. I'm using jQuery's 'unload' event which isn't supported by Opera at all. Neither is onbeforeunload or onunload.

The question is, how do I implement this kind of functionality for Opera browsers?

One solution I had was to make special use of a 'polling' feature I created. This feature allows you to specify an interval which pushes the content to the server every 'x' seconds. Setting this to 1 second specifically for Opera browsers would probably solve this issue, but it's an awful amount of overhead and the requests aren't always completed in sequence, etc ...

Any suggestions or am I only stuck with the above option?

Thanks!

I suppose I could just link you guys to the plugin source. http://www.thedrunkenepic.com/junk/jquery.mousalytics.js

Regarding the code linked above, adding:

if(window.opera)
{
options.interval = 1;
}

On line 89 works great. My only concern is overhead, so I'm still looking for a more elegant solution.

Upvotes: 2

Views: 4479

Answers (3)

gorivo
gorivo

Reputation: 71

I've had the same problem and this saved my day:

if( typeof(opera) != 'undefined' )
{
    opera.setOverrideHistoryNavigationMode( 'compatible' );
    history.navigationMode = 'compatible';
}

More info about this problem can be found at: http://www.opera.com/support/kb/view/827/

Upvotes: 1

davethegr8
davethegr8

Reputation: 11595

According to http://bytes.com/topic/javascript/insights/799229-browser-quirk-onload-onunload-do-not-fire-back-forward-refresh-opera, Opera never really fires onload / onunload events, so functionality like this isn't possible without hacks.

http://dev.opera.com/articles/view/efficient-javascript/?page=4 seems to confirm this, and basically states that opera tries to maintain the state of the page across requests.

On further investgation, http://unitehowto.com/Onunload indicates that it might be possible with opera.io.webserver.addEventListener('_close', onunload, false); (where onunload is a previously defined function), however it also indicates that this functionality is not consistent across all versions of opera, and might not work at all.

I think that your best option is probably to use the polling option for Opera, or possibly use a server-side check for the current page and where it falls in the history queue.

Upvotes: 2

jimyi
jimyi

Reputation: 31191

Does adding this line of JavaScript work for you?

history.navigationMode = 'compatible';

Source: http://www.opera.com/support/kb/view/827/

Upvotes: 1

Related Questions