更好的做法是什么:eval 还是append 脚本?
我需要执行从某个 AJAX 调用中获得的自定义 JavaScript 片段。我可以对字符串进行 eval
操作,也可以将其附加到 DOM 的 script
标签中。哪种方法会更好?
var dynamicScript = 'alert(\'Hello world!\');';
方法 1 - 脚本:
var x = '<script type="text/javascript">' + dynamicScript +'</scr' + 'ipt>';
$(document.body).append(x);
方法 2 - 评估:
eval(dynamicScript);
哪种方法更好,为什么?或者有更好的选择吗?
I need to execute a custom piece of JavaScript I got from some AJAX call. I could do an eval
of the string or I could just append it in a script
-tag to the DOM. Which method would be better?
var dynamicScript = 'alert(\'Hello world!\');';
Method 1 - Script:
var x = '<script type="text/javascript">' + dynamicScript +'</scr' + 'ipt>';
$(document.body).append(x);
Method 2 - Eval:
eval(dynamicScript);
What method is better and why? Or is there an ever better alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢
eval
,因为它通常比创建更快脚本标记,并附加它(特别是如果您想使用 jQuery 创建和插入它)。旁注(脚本标签的有用应用)我还使用脚本标签插入方法:在 Google Chrome 的扩展中,注入脚本标签是在页面范围内运行代码的唯一方法,因为 < code>window 对象已沙箱化。
PS。
jQuery.getScript()
的概念。这个方法可能有用。I prefer
eval
, because it's generally faster than creating a script tag, and appending it (especially if you wanted to create and insert it using jQuery).Side note (useful application of a script tag) I also use the script-tag-insertion method: In Google Chrome's extensions, injecting script-tags is the only way to run code in the scope of a page, because the
window
object is sandboxed.PS. Notion of
jQuery.getScript()
. This method might be useful.将其添加到 DOM。原因(取自 http://ajaxpatterns.org/On-Demand_Javascript ):
JavaScript 将自动评估与第一次遇到标签时评估静态 HTML 中链接的 JavaScript 的方式大致相同。您可以声明一个裸函数或变量,而不将其添加到窗口中。
您可以从外部域加载 JavaScript。
URL 指向特定的 Javascript 资源。使用 XMLHttpRequest 可以提供更大的灵活性:例如,您可以在不同的 XML 节点内发送多个 JavaScript 片段。
DOM 受到不同方式的影响。即使行为是暂时的,脚本也会被添加到 DOM 中,而在 XMLHttpRequest 回调评估它之后它就会消失。
Add it to the DOM. Reasons (taken from http://ajaxpatterns.org/On-Demand_Javascript ):
The JavaScript will automatically be evaluated in much the same way as JavaScript linked in the static HTML is evaluated when the tag is first encountered. You can declare a bare function or variable without adding it to the window.
You can load JavaScript from external domains.
The URL points to a specific Javascript resource. With XMLHttpRequest, there's more flexibility: you can, for example, send several JavaScript snippets inside different XML nodes.
The DOM is affected in a different way. Even if the behaviour is transient, the script will be added to the DOM, whereas it would disappear after an XMLHttpRequest callback eval'd it.
如果 ajax 调用返回带有脚本标记的 html,则可以使用 $.load() 导入脚本。
http://api.jquery.com/load/
If the ajax call is returning html with script tags, you can use $.load() to import the script.
http://api.jquery.com/load/
这两种方法对于您正在做的事情来说都不是那么好。您的 AJAX 调用应该返回数据而不是序列化脚本。您的两种方法都可以让您进行脚本注入。
应该不惜一切代价避免
eval
。它既缓慢又危险,eval 是邪恶的Neither method is really that good for what you're doing. Your AJAX call should be returning data not serialized scripts. Both of your methods open you up to script injection.
eval
should be avioded at all costs. It's slow and dangerous, eval is evil