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

  • Home
  • SEARCH
  • 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 5992959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:36:25+00:00 2026-05-22T23:36:25+00:00

I have seen a snippet like this for detecting IE in JavaScript using conditional

  • 0

I have seen a snippet like this for detecting IE in JavaScript using conditional comments.

var ie = (function(){

    var undef, v = 3, div = document.createElement('div');

    // the while loop is used without an associated block: {}
    // so, only the condition within the () is executed.

    // semicolons arent allowed within the condition,
    //   so a comma is used to stand in for one
    // basically allowing the two separate statements 
    //   to be evaluated sequentially.

    while (
        div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
        div.getElementsByTagName('i')[0]
    );

    // each time it's evaluated, v gets incremented and
    //   tossed into the DOM as a conditional comment
    // the i element is then a child of the div.

    // the return value of the getEBTN call is used as 
    //   the final condition expression
    // if there is an i element (the IE conditional
    //   succeeded), then getEBTN's return is truthy
    // and the loop continues until there is no 
    //   more i elements.

    // In other words:  ** MAGIC**

    return v > 4 ? v : undef;

}());

The above given is the documented (and slightly improved) version by Paul Irish on a snippet by James Padolsey. I am posting the commented version to let you know that I might need a simpler explanation if anybody may.

I would really like to know what happens inside the while loop. I don’t understand that.

  • 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-05-22T23:36:26+00:00Added an answer on May 22, 2026 at 11:36 pm

    (Assuming I haven’t bungled this up horribly) the while loop is equivalent to the following:

    var elt;
    
    do
    {
        v++;
        div.innerHTML = '<!--[if gt IE ' + v + ']><i></i><![endif]-->'
        elt = div.getElementsByTagName('i')[0];
    } (while elt);
    

    does mdc or any good ones cover this while(stmt1, stmt2) thing.

    Here’s what MDC says about while:

    while (condition)
        statement
    

    condition
    An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed. When condition evaluates to false, execution continues with the statement after the while loop.

    We can find out exactly what an expression is in JavaScript from MDC:

    An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value; the value can be a number, a string, or a logical value.

    Conceptually, there are two types of expressions: those that assign a value to a variable, and those that simply have a value. For example, the expression x = 7 is an expression that assigns x the value seven. This expression itself evaluates to seven. Such expressions use assignment operators. On the other hand, the expression 3 + 4 simply evaluates to seven; it does not perform an assignment. The operators used in such expressions are referred to simply as operators.

    If you’re feeling brave, you can also check out the ECMA-262 language specification, specifically the following sections:

    • 11 Expressions, especially 11.14 Comma Operator ( , )
    • 12.6.2 The while Statement

    Sorry I can’t provide direct links as it’s all inside of a PDF.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this problem arise in many different circumstances and would like to
I have seen the references to VistaDB over the years and with tools like
I have seen a few mentions of this idiom (including on SO ): //
I have seen the official demos on lwjgl.org but I would like to see
I have seen the solution for this problem in this thread -> How to
Ok this is the scenario... I have a table in Oracle that acts like
I have factory that looks something like the following snippet. Foo is a wrapper
I have a long snippet of HTML I want some javascript variable to equal
I'm using pyjd/hulahop/pyxpcom to load a HTML document with an input field, this input
I have seen various code snippets which find outs co-ordinates from various methods like

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.