jQuery 如何在head中添加或删除一个js文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12854059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add or remove a js file in head
提问by Charles Yeung
Suppose I have 3 language: en
, tc
, sc
, I want to achieve the below condition
假设我有 3 种语言:en
, tc
, sc
,我想达到以下条件
1) When the current is en
, add en.js
to head and remove js file tc.js
and sc.js
in head
1)当前为 时en
,添加en.js
head并删除js文件tc.js
和sc.js
inhead
2) When the current is tc
, add tc.js
to head and remove js file sc.js
and en.js
in head
2)当前为 时tc
,添加tc.js
head并删除js文件sc.js
和en.js
inhead
3) When the current is sc
, add sc.js
to head and remove js file tc.js
and en.js
in head
3)当前为 时sc
,添加sc.js
head并删除js文件tc.js
和en.js
inhead
Can someone advise how to do that in jQuery?
有人可以建议如何在 jQuery 中做到这一点吗?
Thanks
谢谢
回答by Blake Taylor
Here's a quick function that would load a script dynamically based on a language parameter.
这是一个快速函数,可以根据语言参数动态加载脚本。
addLanguageScript = function(lang) {
var head = document.getElementsByTagName("head")[0],
script = document.createElement('script');
script.type = 'text/javascript'
script.src = lang + '.js'
head.appendChild(script);
};
addLanguageScript('en');
To remove the files you can do something like in jQuery, but keep in mind if the scripts where loaded they probably would have already had some effect.
要删除文件,您可以执行类似于 jQuery 的操作,但请记住,如果加载的脚本可能已经产生了一些效果。
$("script[src='en.js']").remove()
回答by Praveen Kumar Purushothaman
Once the document
is loaded, you cannot do it dynamically. But there's a script require.js, which is a JavaScript file and module loader.
一旦document
被加载,你不能这样做动态。但是有一个脚本require.js,它是一个 JavaScript 文件和模块加载器。
It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.
它针对浏览器内使用进行了优化,但也可以在其他 JavaScript 环境中使用,如 Rhino 和 Node.js。使用像 RequireJS 这样的模块化脚本加载器将提高代码的速度和质量。
It has a good support to use it with jQuery. You may also want to load code on the fly, after page load. Check out the above link and it would surely target your issue.
回答by gkond
Why removing files in first place? If they were in page head it means they were already executed.
为什么首先删除文件?如果它们在页眉中,则意味着它们已经被执行。
With Yesnope JS-module loader the solution will be like:
使用 Yesnope JS 模块加载器,解决方案如下:
yepnope({
test : lang=="en",
yep : ['en.js']
});
BTW, this module-loader is used in Modernizr.
顺便说一句,这个模块加载器用于 Modernizr。
回答by Anup
add and remove dynamically files in head section
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
Call Function:
调用函数:
loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
for remove:
删除:
$('head script[src*="en.js"]').remove(); // for removing dynamically
$('head script[src*="en.js"]').append(); // for append dynamically