Load external js file with jQuery

前端 未结 3 1046
醉酒成梦
醉酒成梦 2021-01-14 01:48

Looked for it everywhere, and found the answer but lost. anyone knows how to load an external .js file from another js file?

main_lobj1.onreadystatechange =          


        
相关标签:
3条回答
  • 2021-01-14 02:23

    Exactly for this reason a function $.getScript() exist in jquery. You can use it simply in this way:

    $.getScript("test.js", function( data, textStatus, jqxhr ) {
      // this is your callback.
    });
    
    0 讨论(0)
  • 2021-01-14 02:40

    This is the way you can do it to load an external js file to Jquery

    $.ajax({
      type: "GET",
      url: "test.js",
      dataType: "script"
    });
    
    0 讨论(0)
  • 2021-01-14 02:42

    If you call an external script you want to base your script on you may want to take note that ajax-based jquery script are asynchronous by defealt.

    Calling an external script asynchronousily will cause the rest of the rest being executed before the external script is loaded.

    Today I ran into the same problem which was easily being solved by making a small adition to Sam Arul Raj's post:

    $.ajax({
      type: "GET",
      url: "test.js",
      dataType: "script",
      async: false
    });
    
    0 讨论(0)
提交回复
热议问题