【问题标题】:Defining variable using eval() shows undefined error使用 eval() 定义变量显示未定义错误
【发布时间】:2012-09-03 13:35:17
【问题描述】:

直接定义变量时,它可以工作。和下面的代码一样,body的背景色在IE中为浅绿色,在非IE浏览器中为浅蓝色。

<html>
<body>
    <script>
        if (window.attachEvent) {
            var YourBrowserIsIE = true;
        }

        if (YourBrowserIsIE) {
            document.body.style.backgroundColor = 'lightgreen';
        }
        else {
            document.body.style.backgroundColor = 'lightblue';
        }
    </script>
</body>
</html>


但是,有时需要使用 eval() 定义变量,如下所示,但结果会显示错误,指出 YourBrowserIsIE 在非 IE 浏览器中未定义。

if (window.attachEvent) {
    eval('var YourBrowserIsIE = true;');
}


是的,我知道我可以为非 IE 浏览器预定义 var YourBrowserIsIE = false; 或将 if 语句更改为 if (typeof YourBrowserIsIE != 'undefined'),但我希望尽可能减少代码。

那么有没有解决方案使用 eval() 定义变量并使用简单的if (YourBrowserIsIE) 检查变量而不在非 IE 浏览器中显示任何错误?


== 编辑 ==

抱歉,不清楚。上面提到的使用eval()的情况,其实是为了检测IE版本。请看以下代码。

<html>
<body>
    <script>
        if (window.attachEvent) {
            var version = /msie (\d+)/i.exec(navigator.userAgent)[1];
            eval('var YourBrowserIsIE' + version + ' = true;');
        }

        if (YourBrowserIsIE9) {
            document.body.style.backgroundColor = 'lightgreen';
        }
        else {
            document.body.style.backgroundColor = 'lightblue';
        }
    </script>
</body>
</html>

【问题讨论】:

  • 不应该 YourBrowserIsIE 在非 IE 浏览器中的两个代码中都未定义吗?只要window.attachEvent 不存在,就永远不会定义YourBrowserIsIE
  • var attachEvent 在全球范围内怎么样?我的浏览器不一定是IE,但你的代码会这样对待。
  • 你认为为什么需要eval
  • 谢谢大家,很抱歉不清楚。我已经编辑了这个问题。
  • 你试过window['YourBrowserIsIE' + version] = true;

标签: javascript variables eval undefined


【解决方案1】:

但我想尽量减少代码

那不是window.YourBrowserIsIE = window.attachEvent;吗?

我看到它有两个优点:

  1. 这是最小的
  2. 不需要eval

看到你的代码,我建议不要使用YourBrowserIsIE,而是使用:

document.body.style.backgroundColor = window.attachEvent 
                                       ? 'lightgreen' : 'lightblue';

看到您的编辑,可能/将是:

document.body.style.backgroundColor = 
              +((/msie (\d+)/i.exec(navigator.userAgent)||[0])[1]) === 9 
                ? 'lightgreen' : 'lightblue'; 

如果它必须是一个可重用的变量,我会抽搐一下回到解决方案 1:

window['YourBrowserIsIE'+((/msie (\d+)/i.exec(navigator.userAgent)||[0])[1]] 
       = true;
document.body.style.backgroundColor = window.YourBrowserIsIE9 ?
                                       ? 'lightgreen' : 'lightblue';

【讨论】:

  • window.YourBrowserIsIE = !!window.attachEvent; 使其成为布尔值。
  • 谢谢。但是,变量 YourBrowserIsIE9 可能会重复使用很多次。
  • 代码在非 IE 浏览器中会抛出错误,因为 [1] 无法读取 null。
  • @Sandreep:仔细看(/msie (\d+)/i.exec(navigator.userAgent) /*==&gt;*/ || [0])[1]
  • 好吧,看来最接近的方法是测试window.YourBrowserIsIE9就像@Kooilnc说的那样。
【解决方案2】:

else case 放入if 条件并尝试:

if (window.attachEvent) {
    eval('var YourBrowserIsIE = true;');
}
else{
    eval('var YourBrowserIsIE = false;');
}

由于您在if (window.attachEvent) 条件内声明变量YourBrowserIsIE,如果上述条件失败,该变量将保持未定义。

【讨论】:

    【解决方案3】:

    正如其他人所建议的,不需要执行 eval。

    无论如何,如果您希望将代码设置为真/假;你可以这样做

    eval('var YourBrowserIsIE = window.attachEvent ? true : false;')
    

    除非您分享实际问题,否则很难提供解决方案。

    【讨论】:

      【解决方案4】:

      忽略这样一个事实,即使用对象推断是检测用户代理的一种严重缺陷的方法(许多浏览器复制 IE 的事件模型),因此极其不可靠,以下是在没有 eval 和 with最少的代码:

      [...呃,见 Kooilnc 的回答...]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-30
        • 1970-01-01
        • 2017-07-03
        相关资源
        最近更新 更多