开发者

Use jQuery to add scripts to the Head of a document

开发者 https://www.devze.com 2023-04-06 07:40 出处:网络
I am looking for a way (using jQuery) that I can add other scripts into the head of a document. The reason I need to do this is because when building a \"template\" for a certain web design applicati

I am looking for a way (using jQuery) that I can add other scripts into the head of a document.

The reason I need to do this is because when building a "template" for a certain web design application开发者_StackOverflow中文版 I am only allowed to insert 1 js file into the head of the document.

Some features that I would like to add to the design require more than 1 js file so I think I would like to have other js files added to the head of the document as the page loads. This would allow me to add the features to the design that I want.

I have looked at jQuery's "getScript" call but am not sure if there is a better way to approach this.


Forget about jQuery for this — Head JS sounds like it's exactly what you want.


Try this:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "http://somedomain.com/script.js";
$("head").append(script);


Take a look at requireJS, it's got a lot more features and handles dependencies really well


KulerGary,

$(document).ready(function() {
    $('head').append("<script src='file.js' type='text/javascript'></script>");
});

I can't test but think its work.

Cya.

0

精彩评论

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