Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The core issue can be seen here <a href="http://jsfiddle.net/Raynos/UxrVQ/" rel="nofollow noreferrer">http://jsfiddle.net/Raynos/UxrVQ/</a> I have yet to find out why IE overwrites window.hiya without checking. </p> <p><strong>[Edit]</strong></p> <p>From the specification. Page 38:</p> <blockquote> <p>For each VariableDeclaration or VariableDeclarationNoIn in the code, create a property of the variable object whose name is the Identifier in the VariableDeclaration or VariableDeclarationNoIn, whose value is undefined and whose attributes are determined by the type of code. If there is already a property of the variable object with the name of a declared variable, the value of the property and its attributes are not changed.</p> </blockquote> <p>A possible explanation could be that in global scope IE differentiates between the <code>window</code> object and the <code>variable object</code> for global scope when declaring variables. Alternatively setting a property on the <code>window</code> object directly might not set the same property on the <code>variable</code> object. If you can find a formal JScript specification or have the source of IE lying around then we can find out exactly what the quirk is.</p> <p><strong>[/Edit]</strong></p> <p>Thanks to @TimDown &amp; @JuanMendes pointing out that's an issue with whether writing a property to the window object is a variable declaration. </p> <p><strong>The Issue:</strong></p> <p>variable declaration gets moved to the top of the block. Even if the code is dead. In IE for some reason it will declare hiya as a local variable even though it classes with the property of the same name stored on window.</p> <p><strong>Explanation:</strong></p> <p>What's happening is that your declaring a variable called hiya. The var statement automatically gets removed to the top of the block. An if statement isn't a block, a function is. So event if the code never gets run in the block the variable still gets declared.</p> <p>In firefox it'll recognise that window.hiya is a declaration of hiya.</p> <p>In IE the declaration in the second script overwrites it</p> <p><strong>What it's actaully doing</strong></p> <p>In firefox:</p> <pre><code>// script block 1 var hiya; // window.hiya counts as a declaration window.hiya = "hiya"; // set // script block 2 if (false) hiya = 1; document.write(...) </code></pre> <p>In IE:</p> <pre><code>// script block 1 window.hiya = "hiya"; // script block 2 var hiya; // redeclared here because window.hiya "isn't" a declaration if (false) hiya = 1; document.write(...) </code></pre> <p>The solution is simply namespacing. You're using the same name in two places and accessing it in two different names. Either use different names or use closures to give local scope.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload