eval()函数和附加脚本标记之间的Javascript差异 [英] Javascript difference between eval() and appending script tags

查看:109
本文介绍了eval()函数和附加脚本标记之间的Javascript差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人能解释一下使用JavaScript的eval()之间的差异,以及另一种方法,如使用jQuery来创建脚本标记,然后追加该元素到页面:

I was wondering if someone could explain the difference between using Javascript's eval(), and another approach, like using JQuery to create script tags and then appending that element to the page:

eval(somecode);

VS

$("<script type='text/javascript'>"+somecode+"</script>").appendTo("head");

不知道这是相关的,但这里的背景:我正在使用一个版本的Drupal的弹出窗口的模块,其基本目的是通过AJAX'ing整个页面请求,并将其追加到很容易地将常规链接到弹出窗口该页面在一个模态窗口。这通常包括外部CSS和JavaScript文件。在努力改善这一切的AJAX加载性能我切换到使用AJAX排队,我改变了外部脚本的一个eval()注入上市有关的替代品。然而,这引起其他各种网页零星Javscript错误。

Not sure if this is relevant, but here's the context: I'm working with a version of the Drupal Popups module whose basic purpose is to easily turn regular links into popups by AJAX'ing an entire page request and appending it to the page in a modal window. This frequently includes external CSS and Javascript files. In an effort to improve the performance of all this AJAX loading I switched over to use AJAX queueing, and I changed an eval() of external scripts into the alternative listed about. However, that caused sporadic Javscript bugs on various other pages.

推荐答案

那么一(尽可能的差异去)是评估返回的前$ P结果$ pssion。

Well one (as far as differences go) is eval will return the result of an expression.

var result = eval('3+4'); // result = 7 

只要你的JavaScript字符串在脚本块的结构,我想提出一个脚本标签中注入它/

As long as your javascript string is in the structure of a script block, i would suggest injecting it within a script tag/

这篇关于eval()函数和附加脚本标记之间的Javascript差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆