简体   繁体   中英

How do I load javascript file that's located in external server dynamically?

I am trying to load a javascript file that's stored in an external server.I tried using plain js to do so in the following manner:

$(document).ready(function(){
    scriptObject = document.createElement('script');
    scriptObject.type = 'text/javascript';
    scriptObject.async = true;
    scriptObject.src="https://seal.verisign.com/getseal?host_name=esewa.com.np&size=S&use_flash=YES&use_transparent=YES&lang=en",
});

$(window).load(function(){
                document.getElementByTagName('head')[0].appendChild(scriptObject);      
            });

Since Js is single threaded and the file loading procEss took a lot of time i tried using ajax and js to do so in the following manner

$(document).ready(function(){
    scriptObject = document.createElement('script');
    scriptObject.type = 'text/javascript';
    scriptObject.async = true;
    $.ajax({
        type: "GET",
        url:"https://seal.verisign.com/getseal?host_name=esewa.com.np&size=S&use_flash=YES&use_transparent=YES&lang=en",
        dataType: "script",
        success: function(data) {
            scriptObject.src= data ;
            $(window).load(function(){
           document.getElementByTagName('head')[0].appendChild(scriptObject);   
            });
        }
    }); 

});

However I am not sure if one can load .js file in the way that i have done with ajax and jquery. Is there a better way for doing this? Any help would be appreciated.

对于该http://api.jquery.com/jquery.getscript/,已经存在出色的jQuery函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM