带有 eval 的 Javascript AJAX 包含文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9013513/
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 AJAX include file witth eval
提问by Tom
Suppose I have
假设我有
1) a HTML document.
1) 一个 HTML 文档。
2) This HTML document loads Javascript file "code.js" like this:
2) 这个 HTML 文件像这样加载 Javascript 文件“code.js”:
<script src="code.js">
3) User clicks button which runs "fetchdata" function in "code.js",
3) 用户点击在“code.js”中运行“fetchdata”函数的按钮,
4) "fetchdata" function looks like this:
4) “fetchdata”函数如下所示:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
myjsdata = xmlhttp.responseText;
}
}
xmlhttp.open("GET", 'http://www.example.com/data.js', false);
xmlhttp.send(null);
...
...
Now how do I do the following successfully:
现在我如何成功执行以下操作:
I want to insert/eval my Javascript in a way, so all functions in "code.js" including "fetchdata" and functions defined above/below can access the data (structures, declarations, pre-calculated data values etc.) in "data.js".
我想以某种方式插入/评估我的 Javascript,因此“code.js”中的所有函数(包括“fetchdata”和上面/下面定义的函数)都可以访问“中的数据(结构、声明、预先计算的数据值等)”数据.js”。
(If this was possible, it would be idea since I could wait loading the actual JS data file until the user explicitly requests it.)
(如果这是可能的,那将是个好主意,因为我可以等待加载实际的 JS 数据文件,直到用户明确请求它。)
回答by mowwwalker
jQuery always has something for everything:
jQuery 总能满足一切需求:
http://api.jquery.com/jQuery.getScript/
http://api.jquery.com/jQuery.getScript/
Loads a javascript file from url and executes it in the global context.
从 url 加载一个 javascript 文件并在全局上下文中执行它。
edit:Oops, didn't see that you weren't using jQuery. Everyone is always using jQuery...
编辑:哎呀,没看到你没有使用 jQuery。每个人都一直在使用 jQuery...
Just do:
做就是了:
var scrpt = document.createElement('script');
scrpt.src='http://www.example.com/data.js';
document.head.appendChild(scrpt);
回答by Joseph
回答by Jeffrey Sweeney
This depends on a lot of factors, but in most cases, you will want to load all of your code/html/css in one sitting. It takes fewer requests, and thus boast a higher perceived performance benefit. Unless your code file is over several Megabytes big, loading it when a user requests it is unnecessary.
这取决于很多因素,但在大多数情况下,您会希望一次性加载所有代码/html/css。它需要更少的请求,因此拥有更高的感知性能优势。除非您的代码文件超过几兆字节,否则在用户请求时加载它是不必要的。
In addition to all of this, modifying innerHTML
and running scripts via eval
can be very cumbersome and risky (respectively). Many online references will back this point. Don't assume that, just because a library is doing something like this, it is safe to perform.
除此之外,修改innerHTML
和运行脚本通过eval
可能非常麻烦和风险(分别)。许多在线参考资料都会支持这一点。不要认为,仅仅因为图书馆正在做这样的事情,就可以安全地执行。
That said, it is entirely possible to load external js files and execute them. One way is to stick all of the code into a newly created script tag. You can also just try running the code in an eval
function call (though it isn't recommended).
也就是说,完全可以加载外部 js 文件并执行它们。一种方法是将所有代码粘贴到新创建的脚本标签中。您也可以尝试在eval
函数调用中运行代码(尽管不推荐这样做)。
address = "testscript.js";
var req = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
if(req == null) {
console.log("Error: XMLHttpRequest failed to initiate.");
}
req.onload = function() {
try {
eval(req.responseText);
} catch(e) {
console.log("There was an error in the script file.");
}
}
try {
req.open("GET", address, true);
req.send(null);
} catch(e) {
console.log("Error retrieving data httpReq. Some browsers only accept cross-domain request with HTTP.");
}