javascript Javascript加载另一个js文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10906836/
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
Javascript to load another js file
提问by Aaron
I am trying to load another JS file from a JS file.
我正在尝试从 JS 文件加载另一个 JS 文件。
From my JavaScript file run.js, I have the following:
从我的 JavaScript 文件run.js,我有以下内容:
document.write("<script type='text/javascript' src='my_script.js'></script>");
alert(nImages);
In side my_script.jsI have the following:
在侧my_script.js我有以下内容:
<SCRIPT language="JavaScript" type="text/javascript">
<!--
nImages = 6;
//-->
</SCRIPT>
But I can't seem to get it to alert the nImages
from my_script.jsfile.
但我似乎无法让它nImages
从my_script.js文件中发出警报。
回答by tjscience
You could do this:
你可以这样做:
var script = document.createElement('script');
script.src = 'my_script.js';
script.type = 'text/javascript';
script.onload = function () {
alert(nImages);
};
document.getElementsByTagName('head')[0].appendChild(script);
回答by calvinf
You should not use HTML inside of your script file. Your script file my_script.js should have only this in it.
您不应在脚本文件中使用 HTML。您的脚本文件 my_script.js 中应该只有这个。
nImages = 6;
Additional note: you don't need language="JavaScript"
or the <!--
or //-->
. Those are old conventions not needed for modern browsers (even IE6). I'd also avoid using document.write()
in your JS as it has performance implications. You may want to look at a library such as RequireJSwhich provides a better way to load other JS files in the page.
附加说明:您不需要language="JavaScript"
or<!--
或//-->
。这些是现代浏览器(甚至 IE6)不需要的旧约定。我也会避免document.write()
在您的 JS 中使用,因为它会影响性能。你可能想看看像RequireJS这样的库,它提供了一种更好的方法来加载页面中的其他 JS 文件。
I also have a code snippet on Githubinspired by Steve Souders that loads another file via straight JS.
我在 Github 上还有一个受 Steve Souders 启发的代码片段,它通过直接 JS 加载另一个文件。
var theOtherScript = 'http://example.com/js/script.js';
var el = document.createElement('script');
el.async = false;
el.src = theOtherScript;
el.type = 'text/javascript';
(document.getElementsByTagName('HEAD')[0]||document.body).appendChild(el);
This will append the other script to the element (if it exists) or the of the page.
这会将另一个脚本附加到元素(如果存在)或页面的 。
回答by Paul
Javascript files should not have HTML in them. They should consist entirely of Javascript code, so my_script.js
should contain only:
Javascript 文件中不应包含 HTML。它们应该完全由 Javascript 代码组成,so my_script.js
应该只包含:
nImages = 6;
This still won't work because when you write the new script tag into the document it doesn't run immediately. It is guaranteed that run.js
finishes running before my_script.js
starts, so nImages
is undefined when you alert it and then becomes 6 later. You'll see that this works:
这仍然不起作用,因为当您将新的脚本标记写入文档时,它不会立即运行。保证run.js
在my_script.js
开始之前完成运行,所以nImages
当你警告它时是 undefined ,然后变成 6 。你会看到这是有效的:
document.write("<script type='text/javascript' src='my_script.js'></script>");
function call_on_load(){
alert(nImages);
}
If the contents of my_script.js
are:
如果内容my_script.js
是:
nImages = 6;
call_on_load();
Edit
编辑
Since you said in a comment that you can not edit my_script.js
you can do this although it is not nearly as nice a solution:
由于您在评论中说您无法编辑,因此my_script.js
您可以执行此操作,尽管它不是一个很好的解决方案:
// Force nImages to be undefined
var undefined;
window.nImages = undefined;
document.write("<script type='text/javascript' src='my_script.js'></script>");
(function is_loaded(cb){
if(typeof window.nImages == 'undefined')
setTimeout(function(){ is_loaded(cb); }, 100);
else
cb();
})(function(){
// This is executed after the script has loaded
alert(nImages);
});
This is not a nice solution, however, since it will continue polling indefinitely if there is an error loading the script.
然而,这不是一个好的解决方案,因为如果加载脚本出现错误,它将无限期地继续轮询。
EDIT
编辑
You posted in a comment the file you want to include, which has the <SCRIPT
at the top. This file is useless and you can't do anything about it client side. You'd have to write a server side script to load the file as text in which case you can just parse it for the value you want.
您在评论中发布了要包含的文件,该文件<SCRIPT
位于顶部。这个文件是没用的,你不能在客户端做任何事情。您必须编写一个服务器端脚本以将文件作为文本加载,在这种情况下,您只需将其解析为所需的值即可。