Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variable oddities in IE [duplicate]

Possible Duplicate:
Why does IE nuke window.ABC variables?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
</head>
<body>
    <script>
        if(typeof q === "undefined"){
            window.q = {test: "test"};
        }
        alert("1="+q) 
    </script>
    <script>
        alert("2="+q)
        if(typeof q === "undefined"){
            var q = {};
        }
        alert("3="+q.test)
    </script>
    <script>
        alert("4="+q.test)
    </script>
</body>

In IE8, the result is

1=[object Object]
2=undefined
3=undefined
4=undefined

The second script seems to override the q of window.

If I change the code to window.q = {test: "test"}; of the first script to q={test:"test"}, the result will be the same as other browsers.

Is this a bug in IE?

like image 851
user1039304 Avatar asked Nov 04 '22 12:11

user1039304


1 Answers

Looks like a bug to me. In IE 10, the above yields

1=[object Object]
2=[object Object]
3=test
4=test

This is the same behaviour as Firefox.

EDIT: See also https://stackoverflow.com/a/2635726/1641070 and Why does IE nuke window.ABC variables?

like image 81
Lukas_Skywalker Avatar answered Nov 12 '22 11:11

Lukas_Skywalker