Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:33:30+00:00 2026-06-10T17:33:30+00:00

When defining variable directly, it works. Like the following code, the background color of

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:33:32+00:00Added an answer on June 10, 2026 at 5:33 pm

    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';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the following definition for a variable <xsl:variable name=DataType select=@DataType/> Which works just
I am defining css variable like <style type=text/css> @variables { static:{{STATIC_ROOT_PATH}}; } </style> I
I am defining a variable adc_cmd[9] as a static const unsigned char in my
Is it possible to insert into a table variable without defining the schema? I
I am trying to get jquery to pick up variable that I am defining
When defining a resources in routes.rb in Rails, I have the following problem: My
I'm looking for method to create Vector and push some values without defining variable
in the following code: int utf8len(char* s, int len) { Glib::ustring::iterator p( string::iterator(s) );
I have written the following function: it takes in a variable input_name ; The
When defining an environment variable (on Windows for me, maybe there is a more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.