2

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
  • 1
    "I used IE7 to see the" I'm guessing you meant to put more on that line. Re the demo, with Chrome on Ubuntu I see two alerts, "a" and "b". You should indicate in your question what you expect to see, and what you're seeing instead. Commented Sep 14, 2010 at 21:59
  • 1
    I'm not sure how to best revise the title, but I'd suggest...something... Commented Sep 14, 2010 at 21:59
  • I thought it a spam post :P Evan, are you listening for an onload event before doing anything? It won't solve all of your probs, but it might be enough for you to just wait until the document has fully loaded before making calls to partially downloaded js files. Commented Sep 14, 2010 at 22:02
  • i hope the edit makes it clearer! :) Commented Sep 14, 2010 at 22:11
  • Hi - I updated the code a little differently, but I still get another error. apus.edu/bin/r/u/test.htm Commented Sep 16, 2010 at 18:05

4 Answers 4

6

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 function 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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi - I updated the code a little differently, but I still get another error. apus.edu/bin/r/u/test.htm
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.
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: code.jquery.com/jquery-1.4.2.min.js
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: apus.edu/bin/b/m/version2.htm I think we're getting closer though...
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.
|
3

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?

Comments

0

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;
}

Comments

0

I wrote an real async interface that can be used regardless of Munchkin js has finished loaded or not - https://github.com/ewebdev/marketo-munchkin-async

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.