tags:

views:

115

answers:

3

Hello,

We are including a javascript file from within another javascript file using document.write. Within the first javascript file is a call to a function in the second javascript file. As a result, we are getting an error message: 'gMunchkin' is undefined when I debug the code. What am I doing wrong, and how can 'gMunchkin' be called in this way?

I used IE7 to see the Demo: http://www.apus.edu/bin/r/u/test.htm

+5  A: 

It's very possible the browser hasn't finished downloading munchkin.js when you make the call to mktoMunchkin().

You could use jQuery to load muchkin.js.

$.getScript('http://munchkin.marketo.net/munchkin.js', function() {
     //The code inside this anonymous fuction is executed by $.getScript() when the script has finished 
     //downloading.It is called a Callback function. It is required because 
     //getScript() does not block and will return before the javascript file is 
     //downloaded by the client
     //If the call to getScript was blocking, the client would be frozen until the 
     //js file was downloaded, which could be seconds and could lead the user 
     //to think the browser has crashed
     alert('Muchkin loaded. We can now use the Munchkin library.');
     mktoMunchkin("476-IFP-265");
});
//any code placed here will execute immediately. When this code is executed,
// munchkin.js may not have finished downloading. Hopefully you can see why 
//there is a need for the callback function in $.getScript().

This way you are guaranteed munchkin.js is fully downloaded before trying to use it's functions.

Jamie Carruthers
Hi - I updated the code a little differently, but I still get another error. http://www.apus.edu/bin/r/u/test.htm
Evan
You will still have the same problem. When a script is added to the page, the client still needs to download the file from the server. This could take anything from 10milliseconds to 10seconds! The solution I provided above will work. The code inside function(){} will only be executed when the client has finished downloading the javascript file.
Jamie Carruthers
Evan, I have also added some extra commenting to the code snippet in my answer. Hopefully this will help your understanding of why the $.getScript() method is necessary. If you don't already have jQuery, you can download it here: http://code.jquery.com/jquery-1.4.2.min.js
Jamie Carruthers
Jamie - Thanks for the great detail! I've made the adjustments you recommended. Also, is it possible to include the jquery library within the vendor.js file? We're trying to get all libraries in a vendor.js file. I placed the library on the page and this time when viewed in IE7 for example, the error message says: "Object doesn't support this property or method" Click this link to see the new change: http://www.apus.edu/bin/b/m/version2.htm I think we're getting closer though...
Evan
You can't include jQuery in the vendor.js file, because then the getScript method won't be available. The error IE7 is reporting is in munchkin.js so you will need to discuss with whoever wrote it why it's doint that or check if anyone else has reported it. Also, munckin.js has the jQuery and sizzle (jQuery selector library) code included which is odd and I don't agree with.
Jamie Carruthers
Jamie - Thank you again for your knowledge. I'm reaching out to the vendor for additional info. Is there away to do this without Jquery? I have nothing against jquery. We're trying to launch this within the existing js library and by adding jquery at the page level is something we'd like to avoid.
Evan
Sure. Give the 'Detecting Load Completion' a read: http://unixpapa.com/js/dyna.html
Jamie Carruthers
+3  A: 

When you include another script using document.write, your main script will continue executing, even before the other script has actually been fetched and included. That being said, document.write is deprecated as well and you shouldn't be using it for any purpose at all.

Is there a reason you can't directly add the <script> tag to your HTML?

casablanca
A: 

You could have the parent page do something like

var doAllThisStuff = function() {
    mktoMunchkin();
};
var stillNeedToDoThis = null;
if (typeof mktoMunchkin == "function") {
    doAllThisStuff(); // Yay, we can do it right away!
} else {
    stillNeedToDoThis = doAllThisStuff; // We don't have mktoMunchkin yet. Better wait.
}

Then at the bottom of the new page do something like this

function mktoMunchkin() {
    // All kinds of code
}
if (typeof stillNeedToDoThis == "function") { // is anybody waiting for mktoMunchkin?
    stillNeedToDoThis();
    stillNeedToDoThis = null;
}
Dave Aaron Smith