<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Experiments on ID attributes</title>
<script type="text/javascript">
function foo()
{
    try {
        a = 10
        alert(a)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
</script>
</head>
<body onload="foo()">
<p id="a">
hello, world
</p>
</body>
</html>
This code works fine in Firefox 3.6 and Chrome 5.0.

However, the statement a = 10 causes an error in Internet Explorer 6.0.

To confirm my guess that a indeed behaves like a global constant object, I replaced function foo() with this.
function foo()
{
    try {
        alert(a.innerHTML)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
Indeed, in IE, the alert dialog box displays what I expected.

However, on Firefox it is an error:

One way I could fix the code is by defining function foo in this manner
function foo()
{
    try {
        var a = 10
        alert(a)
    } catch (e) {
        alert(e.name + ': ' +  e.message)
    }
}
Note the var in the declaration.

Realized while writing this note that e.description is not a standard way of getting the error description from the Error object. The description property is defined for Error objects in IE only. The message property is standard and works on IE as well as other browsers.

No comments

Post a comment

RSS