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

javascript - Using eval() to set global variables - Stack Overflow

programmeradmin3浏览0评论

My code to set a global variable using eval is not working. It's as if the assignment is not called at all, but no script errors occur.

<script type="text/javascript">

    $(function() {

        setTimeout(function() {
            eval('var x = 1;');
            alert(x);
        }, 0);
    });
</script>

<div onclick="alert(x);">Click to see 'x'</div>

When the page loads, the alert shows what I expect; it confirms that x = 1. But after that, I click on the div and get a javascript error that x is undefined. How do I make eval add this variable properly?

Background: The code above is a minimally reproducing example from a project I'm working on where we must execute javascript code during AJAX responses. eval works properly most of the time, but this is causing problems.

My code to set a global variable using eval is not working. It's as if the assignment is not called at all, but no script errors occur.

<script type="text/javascript">

    $(function() {

        setTimeout(function() {
            eval('var x = 1;');
            alert(x);
        }, 0);
    });
</script>

<div onclick="alert(x);">Click to see 'x'</div>

When the page loads, the alert shows what I expect; it confirms that x = 1. But after that, I click on the div and get a javascript error that x is undefined. How do I make eval add this variable properly?

Background: The code above is a minimally reproducing example from a project I'm working on where we must execute javascript code during AJAX responses. eval works properly most of the time, but this is causing problems.

Share Improve this question asked Aug 28, 2012 at 21:30 tenfourtenfour 36.9k15 gold badges87 silver badges146 bronze badges 1
  • 1 Why don't you use window.x = 1; instead? – zerkms Commented Aug 28, 2012 at 21:31
Add a ment  | 

3 Answers 3

Reset to default 6

You could use window.eval() to run eval() from global scope. This will assign var as a variable of window, which is what a global variable is: a variable attached to window.

... But you really really shouldn't. eval() is sandboxed for a reason.

That is not unless you really know what you are doing and trust everything you are receiving through XMLHttpRequest. It is one of those chicken/egg things: if you trust the code enough to execute it, it should be programmed well enough to prefix global variables with window. to begin with; thus, you should not need to use window.eval().

Besides, unless you are just trying to avoid async headaches by using the more-manageable XMLHttpRequest (there's a first time for everything...), you really should just create a script tag, assign it's source, and append it as a child to the head or body tag. Dynamically appending script tags is even faster than using XHR, especially for big scripts.

Eval runs locally, you're setting a local variable.

To set a global variable, remove var;

<script type="text/javascript">

    $(function() {

        setTimeout(function() {
            eval('x = 1;');
            alert(x);
        }, 0);
    });
</script>

I wouldn't remend setting global variables, but if you absolutely have to, use the window object:

window['x'] = 1;
发布评论

评论列表(0)

  1. 暂无评论