Hello list, <div><br></div><div>I am seeing some behavior that I cannot explain with a safari 5 extension. First, let me show a bit of code that works as expected outside the extension environment. This loads jQuery from javascript: </div>
<div><br></div><div>(function(){</div><div> var node = document.createElement('script');</div><div> node.onload = function(){</div><div> if(typeof(jQuery) == 'undefined'){</div><div> console.log("fail: jQuery is undefined");</div>
<div> }</div><div> else{</div><div> console.log("success: jQuery is defined");</div><div> }</div><div> };</div><div> node.async = "async";</div><div> node.type = "text/javascript";</div>
<div> node.src = "<a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js</a>"</div><div> document.getElementsByTagName('head')[0].appendChild(node);</div>
<div>})();</div><div><br></div><div><br></div><div>If I open the Safari Console and load a page with this bit of code, I see "success: jQuery is defined". So far so good. Next, I'll copy that exact chunk of code into a file that my safari extension injects, injected.js: </div>
<div><br></div><div>function messageHandler(event){</div><div><div> if(<a href="http://event.name">event.name</a> === "show"){</div><div> (function(){</div><div> var node = document.createElement('script');</div>
<div> node.onload = function(){</div><div> if(typeof(jQuery) == 'undefined'){</div><div> console.log("fail: jQuery is undefined");</div><div> }</div><div> else{</div><div>
console.log("success: jQuery is defined");</div><div> }</div><div> };</div><div> node.async = "async";</div><div> node.type = "text/javascript";</div><div> node.src = "<a href="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js</a>"</div>
<div> document.getElementsByTagName('head')[0].appendChild(node);</div><div> })();</div><div> }</div><div>}</div><div><br></div><div>safari.self.addEventListener("message",messageHandler,false);</div>
</div><div><br></div><div>A tiny bit about my extension: when the user clicks a toolbar button it sends the 'show' message to the injected script. When that happens, I see "fail: jQuery is undefined" logged to the console. If I put a breakpoint inside the onload callback, I can verify that this is the case. However, once I hit continue in the debugger, jQuery is defined! It is as if jQuery is not defined in the scope of my extension when I load it with this method. Does that make sense?</div>
<div><br></div><div>In summary, what would cause the loading method used above to work outside the safari extension environment, but not inside it?</div><div><br></div><div>Thanks, </div><div>Lou Zell</div>