gpt4 book ai didi

javascript - 检查 Ajax 调用中是否加载了 JavaScript?

转载 作者:行者123 更新时间:2023-12-02 18:40:08 25 4
gpt4 key购买 nike

我正在 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 的调用显示“myFunction() 未定义”。问题是,我如何知道它何时被定义?

谢谢!

最佳答案

你可以做到

if (myfunction != undefined)
{
}

此外,为了确保它是一个功能,您可以添加此

if(typeof (window.myFunction) == ‘function’) {

关于javascript - 检查 Ajax 调用中是否加载了 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16922281/

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