检查Ajax调用中是否加载了JavaScript?
我在Ajax调用oncomplete中将JavaScript文件添加到我的站点(使用appendChild)。之后,我尝试调用该文件中包含的函数。像这样:检查Ajax调用中是否加载了JavaScript?,javascript,ajax,Javascript,Ajax,我在Ajax调用oncomplete中将JavaScript文件添加到我的站点(使用appendChild)。之后,我尝试调用该文件中包含的函数。像这样: // append the JS to the head document.getElementsByTagName('head')[0].appendChild(element); // call a function that is contained in the js file myFunction(); 对myFunction的
// append the JS to the head
document.getElementsByTagName('head')[0].appendChild(element);
// call a function that is contained in the js file
myFunction();
对myFunction的调用表示“myFunction()未定义”。问题是,我如何知道它何时被定义
谢谢 你能行
if (myfunction != undefined)
{
}
另外,为了确保它是一个函数,您可以添加以下内容
if(typeof (window.myFunction) == ‘function’) {
您可以使用
onload
来确定它何时完成
var s = document.createElement('script');
s.onreadystatechange = s.onload = function() {
var state = s.readyState;
if (!callback.done && (!state || /loaded|complete/.test(state))) {
// done!
}
};
s.src = "myurl/myscript.js";
document.getElementsByTagName('head')[0].appendChild(s);
tes如果没有定义,可能?
typeof(myFunction)==“undefined”
One word:IE:如果myFunction没有定义,您的第一个示例将抛出一个错误。第二个更好:-)