views:

326

answers:

4

I am trying to understand an issue where event-listener registration on plugins doesn't work in Opera unless i delay them.

In particular, this doesn't work:

document.onload = function() {
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

while delaying the addEventListener() call somewhat through e.g. an alert() does:

document.onload = function() {
    alert('onload()');
    plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
}

It seems that plugins are only loaded after document.onload.

As a non-web-developer, am i missing something simple here? Or is this a known Opera problem with a common work-around?

A: 

I don't know much about Opera but have you tried using jquery's ready function? It's purpose is to add a function you want executed once the DOM is fully loaded and it should work cross browser.

$(document).ready(function() {
 plugin.addEventListener("foo", function() { alert('onFoo'); }, false);
});

More info about the ready function can be found here

Matt Dearing
I don't want additional dependencies for users of plugins (i'm working on an open-source plugin-framework, not trying to fix a certain website).
Georg Fritzsche
+1  A: 

Hi, in general the timing of plugin initialisation, script execution and document event handling isn't well specified, meaning browsers are likely to do different things.

In this case it sounds like you need to make sure the plugin is initialised before you add the listener. One way to do that would be to check for a property the plugin will define (for example, if it was a Flash plugin you could check if PercentLoaded was defined to see if it is ready for scripting.) If not ready for scripting, you could use a timeout to try again a little bit later.

At Opera we've been trying to align with the majority of the other browsers in this area recently, and Opera 10.50 may be working better for you. I'm not sure if we have things fully under control yet though - it would be interesting to hear from you whether behaviour changed in 10.50.

hallvors
Side note: please use window.onload and not document.onload, Opera doesn't support that anymore because it turned out the other browsers didn't.. http://my.opera.com/hallvors/blog/2009/05/20/the-day-supporting-document-onload-became-a-bug :-/
hallvors
Just tested it with 10.50, the issue reamins. I guessed that initialization order isn't well defined, but sadly Opera breaks with the behaviour other browsers have here (i.e. all others work fine). This is one of the test-pages: http://code.google.com/p/firebreath/source/browse/examples/BasicMediaPlayer/test.html *(btw, document.onload isn't something i really use, just wanted to cut down on the example)*
Georg Fritzsche
If you see Opera behaving differently from other browsers please report it at https://bugs.opera.com/wizard/ I don't know if any browsers guarantee that a plugin is initialised before the load event - this is something we at Opera should investigate and probably change if we can figure out what the others are doing.
hallvors
I did, just never got any feedback.
Georg Fritzsche
ah, DSK-281650 I presume. I'll bring this to the attention of a few key people, so let's see what happens.
hallvors
A: 

Thanks for the quick advice, we are using jquery and other things, and this is my result.

I have changed the

$(document).ready(function(){

to:

$(window).ready(function(){

I also changed the order of the js in the html file and put the load stuff to the top of the js bracket and it appears to be working now in Opera, as with the other browsers too.

steve m
+1  A: 

We have further improved handling of this in Opera 10.60, so that behavior is much closer to the other browsers wrt. plug-in initialization and script readyness. I believe the original approach should work now.

Ola P. Kleiven
Thanks for the feedback here, i will check it out when i have the time.
Georg Fritzsche
Indeed, this seems to be resolved with current Opera versions.
Georg Fritzsche