Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的JavaScript代码赢了';在jqueryajax调用之后运行_Javascript_Jquery_Dom Events - Fatal编程技术网

我的JavaScript代码赢了';在jqueryajax调用之后运行

我的JavaScript代码赢了';在jqueryajax调用之后运行,javascript,jquery,dom-events,Javascript,Jquery,Dom Events,script1.js文件中的jQuery代码 $(document).ready(()=>{ $.ajax({ method:"GET", url:"someurl", success : (data,status,xhr)=>{ //Create some html buttons after I get the data //I want to append the js fi

script1.js文件中的jQuery代码

$(document).ready(()=>{
    $.ajax({
       method:"GET",
       url:"someurl",
       success : 
       (data,status,xhr)=>{
         //Create some html buttons after I get the data

         //I want to append the js file here(the JavaScript file adds some event functions to the newly created buttons.I know i can add the event listeners to the button directly here but for some reason I prefer to have the code in a separate js file) 
        //This does not work
          $('head'). append('<script src="script_here"></script>')

        }
     })
})
我已经尝试通过jQuery附加script2.js文件,但它不起作用


为了便于调试,我不想将所有内容都放在一个文件中,我需要在我的项目中同时使用vanilla js和jQuery。

Ajax是异步的,这意味着当您定义Ajax请求时,js解释器将在请求后立即执行代码。当Ajax请求完成时,应该加载第二个脚本。换句话说,您可以将
$('head')移动。将('')
附加到
(数据、状态、xhr)=>{
。此外,您还可以尝试使用document.createElement加载JS文件。示例:

var script2 = document.createElement("script");
script2.src = "PATH OF YOUR JS FILE";
document.head.appendChild(script2);

如果您只想在ajax调用之后加载一个外部js文件,那么我将向您展示如何在纯js中使用。因为我遇到了类似的问题,所以它可能会对您有所帮助

(data,status,xhr)=>{
    const fileref = document.createElement('script');
    fileref.setAttribute('type', 'text/javascript');
    fileref.setAttribute('src',
    'https://speechanywhere.nuancehdp.com/3.0/scripts/Nuance.SpeechAnywhere.js?_r=' +
    (Math.random() * Math.pow(10, 18)).toString(32)); // the math random will help from cache errors each time you load
    document.getElementsByTagName('head')[0].appendChild(fileref);
}

在您的第二个脚本将其事件处理程序添加到文档中之后,很可能没有发生DOMContentLoaded事件。load事件也将不起作用..我在浏览器中也收到一条警告(主线程上的Synchronous xmlhttprequest jQuery.js因对最终用户体验的不利影响而被弃用)当我有没有加载DOMContentLoaded和Load Events的脚本时,请备份一秒钟。为什么您认为您需要该逻辑的事件处理程序,而不仅仅是运行该逻辑?在script2.js中,您的意思是?是的,您为什么需要它?第二个脚本的包含已经包含在成功回调
文档中。head
已经存在可访问。与
document.body相同
(data,status,xhr)=>{
    const fileref = document.createElement('script');
    fileref.setAttribute('type', 'text/javascript');
    fileref.setAttribute('src',
    'https://speechanywhere.nuancehdp.com/3.0/scripts/Nuance.SpeechAnywhere.js?_r=' +
    (Math.random() * Math.pow(10, 18)).toString(32)); // the math random will help from cache errors each time you load
    document.getElementsByTagName('head')[0].appendChild(fileref);
}