开发者

Load javascript via jQuery ajax

开发者 https://www.devze.com 2022-12-10 21:31 出处:网络
Is it possible to use jQuery %Json开发者_开发知识库 function to load a javascriptfile.js asynchronously?$.getScript(\"/path/to/script.js\")If you\'re trying to load an external JavaScript file then yo

Is it possible to use jQuery %Json开发者_开发知识库 function to load a javascriptfile.js asynchronously?


$.getScript("/path/to/script.js")


If you're trying to load an external JavaScript file then you should use Ramon's suggestions.

But if you're for sure that the response format is JSON you could use the getJSON which loads JSON data using an HTTP GET request.

jQuery.getJSON( url, [data], [callback] ) 


Here's pure JavaScript coding that downloads a JavaScript file and put it in the header page itself (if you plan to have a very lightweight page that is instantaneously visible then download JavaScript file in the background) :

var getheadTag = document.getElementsByTagName('head')[0];
setjs = document.createElement('script');
setjs.setAttribute('type', 'text/javascript');
getheadTag.appendChild(setjs);
setjs.text = x.responseText;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号