Saturday, September 16, 2006

For ages, my newer version of my lame Javascript text adventure game has been getting an error in IE6. (Link goes to old version). Sadly, because I had done a lot of changes testing just on Firefox, and the error message was completely useless ('Error: object expected' with the file stated being the HTML file and the line number being the line at which JS is called) it took me a while to figure it out which change had caused the error.

So I just continued developing for Firefox.

This, of course, means that many more IE nonworkinesses have appeared, and since I don't keep detailed version history, it will be painful to identify them all. But I've finally found the original idiocy.

In Javascript--much like Java, C, C++, and so on--you define a constant by writing const variable_name[ = value];. This works just fine in Firefox and Opera, and probably in most others. But In IE, it doesn't. And when it doesn't work, it doesn't complain 'You wrote "const" on line 42 and I don't know what that means'. Instead, it pretends everything is all right. Suppose you have this in your header:

<script type="text/javascript">
const c = 3;
alert('Testing');
</script>
No error message will occur. Yet should you comment out const c = 3;, the alert will run, so there's no question that IE is doing stuff in the right order here. Same thing if you do
<script type="text/javascript">
function foo()
{
const c = 3;
alert('Testing');
}
</script>
<body onload="foo();">
</body>
No error message. Just silence. Commenting out the const (or doing s/const/var/) will make it work fine.

To get it to report the error, the user has to activate it, as best I can tell. Onclick will get the job done:
<script type="text/javascript">
function foo()
{
const c = 3;
alert('Testing');
}
</script>
<body>
<p onclick="foo();">Get an error message</p>
</body>
And you don't need to call the 'erroneous' function:
<script type="text/javascript">
function foo()
{
const c = 47;
alert(c);
}
function bar()
{
var c = 42;
alert(c);
}
</script>
<body>
<p onclick="bar();">Get an error message</p>
</body>

Basically, if the keyword 'const' appears anywhere in that Javascript, it's all dead, but you won't be informed until you explicitly try to execute it. (Multiple <script> tags will work independently, as will code in an onclick attribute.)

Ramble, ramble, ramble. My point is, someone please tell me how to get constants to work in IE. I don't really need them, but it's generally considered a good practice. Probably slightly faster run-time too, though so minor that it doesn't really matter in this situation.

Anyhow, there will likely be a new version coming out in a few days. Unless I decide otherwise. Or I end up having too much trouble with all the other errors IE6 is--oh, wait, hey! No other errors at first glance! Marvy!

3 comments:

Leif K-Brooks said...

I'm getting syntax errors (with weird line numbers: IE seems to get them from a random number generator) for all of your examples with IE 6.0 on Win2k. Might be because I've got Microsoft Script Debugger installed. I don't have a Script Debugger-less Windows box, so I can't test that hypothesis.

LKBM said...

Oh, cool. I should try that. (Worst. Article. Ever until I fixed it up slightly.)

And I just noticed that it always says '0 comments' on my blog index. I guess I should go fix that, eh? Though it's correct most of the time.

LKBM said...

Well, I tried IRC and got two solutions:

(19:26:26) Dashiva: You could, you know, not change it

(19:27:25) gorzano: lkbm: you can wrap it in an object and only provide a getter method for it
(19:27:43) gorzano: overkill, but heh

I think I'll stick to Dashiva's solution. It's worked for meso far.