是否可以使用 AJAX 加载新的 Javascript 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6031660/
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
Is it possible to load new Javascript files with AJAX?
提问by Nick Brunt
I'm building a new AJAX powered website with distinct sections. Each section requires a new set of Javascript functions to run. I'd rather not load every script at the start, as there might be quite a few.
我正在构建一个具有不同部分的新的 AJAX 驱动的网站。每个部分都需要一组新的 Javascript 函数才能运行。我不想在开始时加载每个脚本,因为可能有很多。
Is there a way to load new scripts using AJAX and remove old ones (so as to make sure there are no compatibility issues with similar variable names or function signatures).
有没有办法使用 AJAX 加载新脚本并删除旧脚本(以确保没有类似变量名或函数签名的兼容性问题)。
Thanks
谢谢
Edit- JQuery is fine, it doesn't have to be old school Javascript
编辑- JQuery 很好,它不必是老派的 Javascript
回答by jvenema
Three things:
三件事:
1) Yes, you can load new scripts. You don't even need AJAX in the XHR sense - simply include the script tag in your page dynamically. jQuery provides $.getScript() for things like this, or you can simply create a script element and append it to the head of your document. There are a gazillion examples if you google it.
1) 是的,您可以加载新脚本。您甚至不需要 XHR 意义上的 AJAX - 只需在您的页面中动态包含脚本标记即可。jQuery 为这样的事情提供了 $.getScript() ,或者您可以简单地创建一个脚本元素并将其附加到文档的头部。如果你用谷歌搜索,有无数的例子。
2) Regarding removing the old (and conflicts, etc). Assuming you have control over these function names, just namespace them properly:
2)关于删除旧的(和冲突等)。假设您可以控制这些函数名称,只需正确命名它们:
window.myapp = {};
window.myapp.mysection = {};
window.myapp.myothersection = {};
window.myapp.mysection.myfunction = function(){}
Then you don't have to worry about conflicts.
那么你就不必担心冲突。
3) Be careful about this. The overhead of loading all those scripts on-demand might be more than just compressing and loading the whole thing in one shot anyway.
3)注意这一点。无论如何,按需加载所有这些脚本的开销可能不仅仅是一次压缩和加载整个内容。
回答by scurker
jQuery makes it pretty simple.
jQuery 让它变得非常简单。
$.getScript('myscript.js', function() {
console.debug('Script loaded.');
});
If you can't use jQuery, there's a few more lines to write.
如果你不会使用jQuery,还有几行要写。
var head = document.getElementByTagName('head')[0],
script = document.createElement('script');
script.src = 'myscript.js';
head.appendChild(script);
回答by Cem Kalyoncu
Script elements are also a part of the DOM tree, you can attach and remove as you like.
脚本元素也是 DOM 树的一部分,您可以随意添加和删除。
回答by bart
Yes you can use oldschool Javascript.
是的,您可以使用 oldschool Javascript。
function addScript(src) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = src;
document.getElementsByTagName('head')[0].appendChild(s);
return s; // to remove it later
}
Once a script is loaded, you can remove the tag again (with removeChild), for example, when you load a new section. That's the reason for the return statement at the end of my function. I'm not really certain it is even useful.
加载脚本后,您可以再次删除标记(使用 removeChild),例如,在加载新部分时。这就是我函数末尾的 return 语句的原因。我不确定它甚至有用。
You can add an "onload" attribute to get a warning via a callback when the script is fully loaded, though that probably isn't very cross-platform.
您可以添加“onload”属性以在脚本完全加载时通过回调获得警告,尽管这可能不是非常跨平台。
回答by d4rky
For loading new scripts you should check out LABjs
要加载新脚本,您应该查看LABjs
Unloading them is however not possible, see another StackOverflow poston this issue.
然而,卸载它们是不可能的,请参阅有关此问题的另一篇 StackOverflow 帖子。
回答by nickytonline
Just Google JavaScript on Demand
只是 Google JavaScript 点播
http://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=javascript+on+demand
http://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=javascript+on+demand
For example, jQuery provides $.getScript(), see http://api.jquery.com/jQuery.getScript/
比如jQuery提供了$.getScript(),见http://api.jquery.com/jQuery.getScript/
回答by Bobby Stenly
I've never try it before... but, you can try call a php form with ajax and add the new javascript to a div in body element.. I hope it'll work..
我以前从未尝试过……但是,您可以尝试使用 ajax 调用 php 表单并将新的 javascript 添加到 body 元素中的 div 中。我希望它会起作用。