views:

849

answers:

3

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.

+1  A: 

Does adding this line of JavaScript work for you?

history.navigationMode = 'compatible';

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

jimyi
Ah, yes, I should have mentioned that I tried that as well. No dice.
Wilhelm Murdoch
+1  A: 

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.

davethegr8
Yeah, you're right. I think I'll just force the polling option in Opera browsers to push data every 5 or so seconds. I'm sure for something like this a 5-second max gap is acceptable. Maybe, someday, they'll add support for something like this.Thanks, buddy!
Wilhelm Murdoch
opera.io.webserver APIs are limited to (Opera Unite) Widgets and such, so the suggested solution above is not an answer for general web pages either.
hallvors
+1  A: 

Finally, what was your solution?

Oscar Hiboux
In the end I chose to go with a polling feature for Opera browsers only. Works well enough if you set the polling interval to something reasonable, like 4 or so seconds.Thanks, everybody, for your help!
Wilhelm Murdoch