[url]http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html[/url]这篇文章很有意思。
这种创建script标签并且设置script.text以动态执行JavaScript代码的方法,在IE下与eval和window.execScript的性能差不多,但是[b]在firefox3下性能比eval平均高2.4倍[/b]。
这种创建script标签并且设置script.text以动态执行JavaScript代码的方法,在IE下与eval和window.execScript的性能差不多,但是[b]在firefox3下性能比eval平均高2.4倍[/b]。
// Evalulates a script in a global context
globalEval: function( data ) {
if ( data && /\S/.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
if ( jQuery.support.scriptEval )
script.appendChild( document.createTextNode( data ) );
else
script.text = data;
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}
},