Christian Heilmann

Opera, REST APIs, Module Patterns and generated script nodes

I just came across an annoying thing in Opera. I love using the Module Pattern for designing my JavaScripts and I love generating script nodes on the fly for REST APIs that give me back JSON. The following example using the twitter API works swimmingly on Firefox and Safari (have to check IE later, but I’d be surprised if it didn’t):

However, if you test this in Opera you get an error:

JavaScript – file://localhost/Applications/MAMP/htdocs/operacallbackfail.html
Inline script thread
name: ReferenceError
message: Statement on line 1: Reference to undefined variable: twitterbadge
Line 1 of linked script[{user : {screen_name : “codepo8”,...08”}]);

It seems like the newly generated script node calls the method of the module before the module has been created. In other words, newly generated script nodes halt the execution of the code that generated them. The following example works across browsers, including Opera:

Not too daunting a fix, but it feels wrong to have to have an extra method and another line calling it.

Try it out for yourself: Opera bug with dynamic script nodes and module pattern