0

When defining variable directly, it works. Like the following code, the background color of body will be light green in IE and will be light blue in non-IE browsers.

<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>


However, sometimes the variable need to be defined using eval(), like the following, but the result will show an error saying that YourBrowserIsIE is undefined in non-IE browsers.

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


Yes, I know I can predefine var YourBrowserIsIE = false; for non-IE browsers or change the if statement to if (typeof YourBrowserIsIE != 'undefined'), but I want to keep the code as minimum as possible.

So is there a solution to use eval() to define the variable and check the variable using the straightforward if (YourBrowserIsIE) without showing any error in non-IE browsers?


== EDIT ==

Sorry for being unclear. The situation of using eval() being mentioned above is actually for detecting IE version. Please see the following code.

<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>
4

4 回答 4

4

but I want to keep the code as minimum as possible

Wouldn't that be window.YourBrowserIsIE = window.attachEvent; then?

I see 2 advantages it it:

  1. It's minimal
  2. It doesn't need eval

Seeing your code, i'd suggest to not use YourBrowserIsIE at all, but use:

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

And seeing your edits, that could/would be:

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

And if it has to be a reusable variable, I come back to solution 1 whith a twitch:

window['YourBrowserIsIE'+((/msie (\d+)/i.exec(navigator.userAgent)||[0])[1]] 
       = true;
document.body.style.backgroundColor = window.YourBrowserIsIE9 ?
                                       ? 'lightgreen' : 'lightblue';
于 2012-09-03T05:13:52.517 回答
1

Put an else case to your if condition and try:

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

Since you are declaring the variable YourBrowserIsIE inside the if (window.attachEvent) condition, the variable will be left undefined if the mentioned condition fails.

于 2012-09-03T05:19:08.597 回答
1

As others have suggested there is no need to perform eval.

Anyways, if you are looking to set the code to true/false; you can perform this

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

Unless you share the actual problem, it will be difficult to provide solution.

于 2012-09-03T05:21:03.297 回答
1

Ignoring the fact that using object inference is a seriously flawed way to detect the user agent (lots of browsers copy IE's event model) and is therefore extremely unreliable, here's how you might do what you are trying to do without eval and with minimal code:

[... ugh, see Kooilnc's answer ...]

于 2012-09-03T05:29:54.980 回答