最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Set Variable inside Eval (JavaScript) - Stack Overflow

programmeradmin2浏览0评论

i'm writing a GreaseMonkey script (using JQuery), and i need some variables that are set by a script in the original page, like this:

<script type="text/javascript"> 
    var rData = {"20982211":[1,0,1],"20981187":[8,0,4]};
</script>

I fetch this element from another page and try to eval it, put strangely this doesn't work:

$.get(link_url, null, function(data) {
   alert("1:" + rData);
   eval($(data).find("script").text());
   alert("2:" + rData);
}

The strange thing is on the firebug console it works (i just tried the eval directly on the targetpage without the .get), when i run the script though it doesn't. It gives me "null" in both alerts.

Any ideas?

i'm writing a GreaseMonkey script (using JQuery), and i need some variables that are set by a script in the original page, like this:

<script type="text/javascript"> 
    var rData = {"20982211":[1,0,1],"20981187":[8,0,4]};
</script>

I fetch this element from another page and try to eval it, put strangely this doesn't work:

$.get(link_url, null, function(data) {
   alert("1:" + rData);
   eval($(data).find("script").text());
   alert("2:" + rData);
}

The strange thing is on the firebug console it works (i just tried the eval directly on the targetpage without the .get), when i run the script though it doesn't. It gives me "null" in both alerts.

Any ideas?

Share Improve this question asked May 4, 2011 at 21:07 Fabian ZeindlFabian Zeindl 5,98810 gold badges57 silver badges82 bronze badges 1
  • Why are you running that code through an eval statement when there is no dynamic code? Unless you expect the symbol to be changing names, just reference the symbol. If the symbol changes names, then how would your code know to look for it? – Tejs Commented May 4, 2011 at 21:11
Add a ment  | 

1 Answer 1

Reset to default 5

EcmaScript 5 redefined eval so that it cannot add variable bindings to the enclosing lexical environment.

http://whereswalden./2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/ talks about the problems with eval under ES 3.

Yet at the same time, eval is too powerful. As inline assembly is to C or C++ (at least without the information gcc‘s asm syntax requires), so is eval to JavaScript. In both instances a powerful construct inhibits many optimizations. Even if you don’t care about optimizations or performance, eval‘s ability to introduce and delete bindings makes code that uses it much harder to reason about.

...

eval‘s ability to add bindings is worse. This can make it impossible to say what a name refers to until runtime:

var v;
function test(code)
{
  eval(code);
  return v;
}

Does the v in the return statement mean the global variable? You can’t know without knowing the code eval will pile and run. If that code is "var v = 17;" it refers to a new variable. If that code is "/* psych! */" it refers to the global variable. eval in a function will deoptimize any name in that function which refers to a variable in an enclosing scope. (And don’t forget that the name test itself is in an enclosing scope: if the function returned test instead of v, you couldn’t say whether that test referred to the enclosing function or to a new variable without knowing code.)

One possible solution to your problem is to use a different evaluation construct, e.g. (new Function('alert(rData); ' + ... + '; alert(rData);')) introduces a plete lexical environment.

发布评论

评论列表(0)

  1. 暂无评论