I have an HTML + JavaScript block widget that people can copy / paste into their HTML page one or more times. This block checks whether the external JavaScript file is already in the DOM and loads it, if not, something like this:
(function(){ d = document; if (!d.getElementById('ex-scr')) { scr = d.createElement('script'); scr.async = true; scr.id = 'ex-scr'; scr.src = 'external.js'; d.getElementsByTagName('head')[0].appendChild(scr) } })();
An external JavaScript file checks the HTML page for widget instances (using getElementsByClassName) and does things with these instances, sort of like:
for (var i=0;i<document.getElementsByClassName('target').length;i++) { document.getElementsByClassName('target')[i].style.borderStyle="solid"; }
A working example of this can be found at http://futtta.be/opera_enigma.html .
This works fine in Firefox (3.6 and 4b), Chrome (5 and 6) and Safari, but it doesnβt work in Opera (tested with the latest version 10.61): no matter how many widgets (divs with class='target' ) , Opera only acts on the first, because, apparently, nodeList contains only 1 entry (the length is 1 instead of 2 or 3 or ...).
The problem disappears if in the javascript of the widget I call a function to load an external script using window.onload, but I would like my widget to be activated as soon as possible (without interfering with the rest of the page, therefore, asynchronous material).
So, my questions; Is there a bug in my code that Firefox, Safari and Chrome ignore? Is this a bug in Opera? How can I make Opera behave?
javascript opera
futtta
source share