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(&#39;script&#39;);</div><div>  node.onload = function(){</div><div>    if(typeof(jQuery) == &#39;undefined&#39;){</div><div>      console.log(&quot;fail: jQuery is undefined&quot;);</div>
<div>    }</div><div>    else{</div><div>      console.log(&quot;success: jQuery is defined&quot;);</div><div>    }</div><div>  };</div><div>  node.async = &quot;async&quot;;</div><div>  node.type = &quot;text/javascript&quot;;</div>
<div>  node.src = &quot;<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>&quot;</div><div>  document.getElementsByTagName(&#39;head&#39;)[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 &quot;success: jQuery is defined&quot;.  So far so good.  Next, I&#39;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> === &quot;show&quot;){</div><div>    (function(){</div><div>      var node = document.createElement(&#39;script&#39;);</div>
<div>      node.onload = function(){</div><div>        if(typeof(jQuery) == &#39;undefined&#39;){</div><div>          console.log(&quot;fail: jQuery is undefined&quot;);</div><div>        }</div><div>        else{</div><div>
          console.log(&quot;success: jQuery is defined&quot;);</div><div>        }</div><div>      };</div><div>      node.async = &quot;async&quot;;</div><div>      node.type = &quot;text/javascript&quot;;</div><div>      node.src = &quot;<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>&quot;</div>
<div>      document.getElementsByTagName(&#39;head&#39;)[0].appendChild(node);</div><div>    })();</div><div>  }</div><div>}</div><div><br></div><div>safari.self.addEventListener(&quot;message&quot;,messageHandler,false);</div>
</div><div><br></div><div>A tiny bit about my extension: when the user clicks a toolbar button it sends the &#39;show&#39; message to the injected script.  When that happens, I see &quot;fail: jQuery is undefined&quot; 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>