<p>I am working with both <a href="http://activemq.apache.org/ajax.html" rel="nofollow noreferrer">amq.js</a> (ActiveMQ) and <a href="http://code.google.com/apis/maps/documentation/reference.html" rel="nofollow noreferrer">Google Maps</a>. I load my scripts in this order</p>

<pre><code>&lt;head&gt;
    &lt;meta http-equiv="content-type" content="text/html;charset=UTF-8" /&gt;
    &lt;title&gt;AMQ &amp; Maps Demo&lt;/title&gt;

    &lt;!-- Stylesheet --&gt;
    &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;&lt;/link&gt;

    &lt;!-- Google APIs --&gt;
    &lt;script type="text/javascript" src="http://www.google.com/jsapi?key=abcdefg"&gt;&lt;/script&gt;

    &lt;!-- Active MQ --&gt;
    &lt;script type="text/javascript" src="amq/amq.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;amq.uri='amq';&lt;/script&gt;

    &lt;!-- Application --&gt;
    &lt;script type="text/javascript" src="application.js"&gt;&lt;/script&gt;
&lt;/head&gt;
</code></pre>

<p>However in my application.js it loads Maps fine but I get an error when trying to subscribe to a Topic with AMQ. AMQ depends on prototype which the error console in Firefox says object is not defined. I think I have a problem with using the amq object before the script is finished loading. <strong>Is there a way to make sure both scripts load before I use them in my application.js?</strong> </p>

<p>Google has this nice function call <code>google.setOnLoadCallback(initialize);</code> which works great. I'm not sure amq.js has something like this.</p>

[{'answer_id': 19067, 'author': 'maxsilver', 'author_id': 1477, 'author_profile': 'https://Stackoverflow.com/users/1477', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p><strong>Is there a way to make sure both scripts load before I use them?</strong></p>\n</blockquote>\n\n<p>Yes.</p>\n\n<p>Put the code you want loaded last (your <code>application.js</code> stuff) into prototype\'s <a href="http://www.prototypejs.org/api/document/observe" rel="nofollow noreferrer">document.observe</a>. This should ensure that the code will load only after prototype + other stuff is finished and ready. (If you are familiar with jQuery, this function is similar to jQuery\'s <code>$(document).ready</code> )</p>\n'}, {'answer_id': 19069, 'author': 'danb', 'author_id': 2031, 'author_profile': 'https://Stackoverflow.com/users/2031', 'pm_score': 3, 'selected': False, 'text': '<p>in jquery you can use: </p>\n\n<pre><code>$(document).ready(function(){/*do stuff here*/});\n</code></pre>\n\n<p>which makes sure the javascript is loaded and the dom is ready before doing your stuff.</p>\n\n<p>in prototype it looks like this might work</p>\n\n<pre><code>document.observe("dom:loaded", function() {/*do stuff here*/});\n</code></pre>\n\n<p>If I understand your problem correctly.. I think that may help..</p>\n\n<p>If you don\'t want to rely on a lib to do this... I think this might work:</p>\n\n<pre><code>&lt;script&gt;\n   function doIt() {/*do stuff here*/}\n&lt;/script&gt;\n&lt;body onLoad="doIt();"&gt;&lt;/body&gt;\n</code></pre>\n'}, {'answer_id': 19276, 'author': 'Walter Rumsby', 'author_id': 1654, 'author_profile': 'https://Stackoverflow.com/users/1654', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>AMQ depends on prototype which the error console in FireFox says object is not defined.</p>\n</blockquote>\n\n<p>Do you mean that AMQ depends on the <a href="http://www.prototypejs.org/" rel="nofollow noreferrer">Prototype library</a>? I can\'t see an import for that library in the code you\'ve provided.</p>\n'}, {'answer_id': 19348, 'author': 'Walter Rumsby', 'author_id': 1654, 'author_profile': 'https://Stackoverflow.com/users/1654', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n  <p><strong>Is there a way to make sure both scripts load before I use them in my application.js?</strong></p>\n</blockquote>\n\n<p>JavaScript files should load sequentially <em>and block</em> so unless the scripts you are depending on are doing something unusual all you should need to do is load application.js after the other files.</p>\n\n<p><a href="http://yuiblog.com/blog/2008/07/22/non-blocking-scripts/" rel="noreferrer">Non-blocking JavaScript Downloads</a> has some information about how scripts load (and discusses some techniques to subvert the blocking).</p>\n'}, {'answer_id': 20504, 'author': 'Bernie Perez', 'author_id': 1992, 'author_profile': 'https://Stackoverflow.com/users/1992', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>Do you mean that AMQ depends on the\n  Prototype library? I can't see an\n  import for that library in the code\n  you've provided.</p>\n</blockquote>\n\n<p>Yes for ActiveMQ's javascript (amq.js) does depend on Prototype. In the amq.js it loads 3 scripts, _amq.js, behaviour.js and prototype.js.</p>\n\n<p>Thanks you for your help on the JavaScript load order wrumsby. This tells me that my bug is in another castle :(</p>\n\n<p>I guess I have a different problem. I also checked the js files from ActiveMQ 5.0 to 5.1 and noticed they were the same as well. Something has changed in 5.0 to 5.1 that requires a refresh for the topics to subscribe. I'll keep looking, but thanks for eliminating this possible cause.</p>\n"}, {'answer_id': 601866, 'author': 'cmcginty', 'author_id': 64313, 'author_profile': 'https://Stackoverflow.com/users/64313', 'pm_score': 3, 'selected': False, 'text': '<p>I had a similar problem to this, only with a single script. The solution I came up with was to use <code>addEventListener("load",fn,false)</code> to a <code>script</code> object created using <code>document.createElement(\'script\')</code> Here is the final function which loads any standard JS file and lets you add a "post load" script.</p>\n\n<pre><code>function addJavaScript( js, onload ) {\n   var head, ref;\n   head = document.getElementsByTagName(\'head\')[0];\n   if (!head) { return; }\n   script = document.createElement(\'script\');\n   script.type = \'text/javascript\';\n   script.src = js;\n   script.addEventListener( "load", onload, false );\n   head.appendChild(script);\n}\n</code></pre>\n\n<p>I hope this may help someone in the future.</p>\n'}, {'answer_id': 5147936, 'author': 'John Doe', 'author_id': 638415, 'author_profile': 'https://Stackoverflow.com/users/638415', 'pm_score': 5, 'selected': False, 'text': '<p>cross-domain scripts are loaded after scripts of site itself, this is why you get errors. interestingly, nobody knows this here.</p>\n'}, {'answer_id': 11761498, 'author': 'Brian Scott', 'author_id': 135731, 'author_profile': 'https://Stackoverflow.com/users/135731', 'pm_score': 0, 'selected': False, 'text': '<p>You can also use the built in SharePoint javascript method to control the execution of your scripts;</p>\n\n<pre><code>_spBodyOnLoadFunctionNames.push("yourFunction");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/19035', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1992/']