我们正在使用document.write从另一个JavaScript文件中包含一个JavaScript文件。在第一个javascript文件中是对第二个javascript文件中的函数的调用。结果,我们收到一条错误消息:调试代码时'gMunchkin'未定义。我在做什么错,如何以这种方式调用“gMunchkin”?

我使用IE7观看了演示:http://www.apus.edu/bin/r/u/test.htm

最佳答案

当您调用mktoMunchkin()时,很有可能浏览器尚未完成下载munchkin.js。

您可以使用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().

这样,您可以确保在尝试使用munchkin.js的功能之前已将其完全下载。

09-10 12:10
查看更多