gpt4 book ai didi

javascript - 从另一个文件包含的文件运行 javascript 函数

转载 作者:行者123 更新时间:2023-12-04 02:18:16 24 4
gpt4 key购买 nike

我们使用 document.write 从另一个 javascript 文件中包含一个 javascript 文件。在第一个 javascript 文件中调用了第二个 javascript 文件中的函数。结果,我们收到一条错误消息:调试代码时未定义“gMunchkin”。我做错了什么,“gMunchkin”怎么能这样称呼?

我用IE7看Demo: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 在尝试使用它的功能之前已经完全下载。

关于javascript - 从另一个文件包含的文件运行 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com