views:

4077

answers:

6

Hey I am working with both amq.js (ActiveMQ) and Google Maps. I load my scripts in this order

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

    <!-- Stylesheet -->
    <link rel="stylesheet" type="text/css" href="style.css"></link>

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

    <!-- Active MQ -->
    <script type="text/javascript" src="amq/amq.js"></script>
    <script type="text/javascript">amq.uri='amq';</script>

    <!-- Application -->
    <script type="text/javascript" src="application.js"></script>
</head>

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. Is there a way to make sure both scripts load before I use them in my application.js?

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

+2  A: 

Is there a way to make sure both scripts load before I use them?

Yes.

Put the code you want loaded last (your application.js stuff) into prototype's document.observe. 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 $(document).ready )

maxsilver
+3  A: 

in jquery you can use:

$(document).ready(function(){/*do stuff here*/});

which makes sure the javascript is loaded and the dom is ready before doing your stuff.

in prototype it looks like this might work

document.observe("dom:loaded", function() {/*do stuff here*/});

If I understand your problem correctly.. I think that may help..

If you don't want to rely on a lib to do this... I think this might work:

<script>
   function doIt() {/*do stuff here*/}
</script>
<body onLoad="doIt();"></body>
danb
+1  A: 

AMQ depends on prototype which the error console in FireFox says object is not defined.

Do you mean that AMQ depends on the Prototype library? I can't see an import for that library in the code you've provided.

wrumsby
+4  A: 

Is there a way to make sure both scripts load before I use them in my application.js?

JavaScript files should load sequentially and block 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.

Non-blocking JavaScript Downloads has some information about how scripts load (and discusses some techniques to subvert the blocking).

wrumsby
A: 

Do you mean that AMQ depends on the Prototype library? I can't see an import for that library in the code you've provided.

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.

Thanks you for your help on the JavaScript load order wrumsby. This tells me that my bug is in another castle :(

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.

Bernie Perez
+2  A: 

I had a similar problem to this, only with a single script. The solution I came up with was to use addEventListener("load",fn,false) to a script object created using document.createElement('script') Here is the final function which loads any standard JS file and lets you add a "post load" script.

function addJavaScript( js, onload ) {
   var head, ref;
   head = document.getElementsByTagName('head')[0];
   if (!head) { return; }
   script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = js;
   script.addEventListener( "load", onload, false );
   head.appendChild(script);
}

I hope this may help someone in the future.

Casey
Thanks, this is helpful.
Jon