weird IE bug

L

lofty00

I found what looks like a definite bug in IE. If you load the page
below in Firefox, the alert comes up as you would expect. If you run it
in IE6 (with sp2), the alert doesn't come up. commenting out the line
that starts 'div.class' makes it work. This is very weird, as far as I
can see - the code is not run, and it's not generating a syntax error,
but it's still preventing the rest of the program from running. All I
can guess is that the javascript parser has crashed on that line for
some reason without returning an error. It is possible to work round
this using setAttribute, but it's annoying nonetheless.

<html>
<head><title>bug test</title>
<script type="text/javascript">
function notCalled() {
var div=document.createElement("div");
div.class="tabContentPaneDiv";
}

alert("here");
</script>
</head>
<body>
<h1>IE Bug Test</h1>
</body>
</html>
 
W

web.dev

div.class="tabContentPaneDiv";

There is no bug. To alter a class for an element, the correct syntax
is:

element.className = "classname";

Changing it to the above will cause your alert to show up.
 
L

lofty00

web.dev said:
There is no bug. To alter a class for an element, the correct syntax
is:

element.className = "classname";

Changing it to the above will cause your alert to show up.

OK - I had the wrong attribute name for what I was originally trying to
do, but there is still a bug as far as I can see. If you change the
line above it so the function reads:

function notCalled() {
var div={class:0};
div.class="tabContentPaneDiv";
}

The alert doesn't show up. It does in Firefox. This shouldn't happen I
think.

The point is the function is never called, so the only time the
javascript interpreter should look at it is when checking for syntax.
At this point, the interpreter shouldn't be bothered whether the div
variable is a dom node or some other kind of object. But even though
it's not called, assigning to the property name 'class' causes the
program never to be run.
 
L

lofty00

OK - I had the wrong attribute name for what I was originally trying to
do, but there is still a bug as far as I can see. If you change the
line above it so the function reads:

function notCalled() {
var div={class:0};
div.class="tabContentPaneDiv";
}

The alert doesn't show up. It does in Firefox. This shouldn't happen I
think.

The point is the function is never called, so the only time the
javascript interpreter should look at it is when checking for syntax.
At this point, the interpreter shouldn't be bothered whether the div
variable is a dom node or some other kind of object. But even though
it's not called, assigning to the property name 'class' causes the
program never to be run.

I just found out that 'class' is a reserved word in javascript, which
may explain this, though if it is, the interpreter should give a syntax
error, not just stop. This also doesn't explain why it runs OK in
firefox.
 
M

Matt Kruse

function notCalled() {
var div={class:0};
div.class="tabContentPaneDiv";
}
The alert doesn't show up. It does in Firefox. This shouldn't happen I
think.

I think IE behaves more appropriately.
'class' is a reserved word and can't be used in the manner you are using it.
A decent JS editor will highlight the word and you would know something is
wrong. Further, I *do* get an error in IE. Perhaps your errors are set to
not pop up.

Try this code:

function notCalled() {
var div={'class':0};
div['class']="tabContentPaneDiv";
}
alert ('x');

Works just fine.
 
R

Randy Webb

Tony said the following on 6/12/2006 8:58 PM:
"It would be nice if IE did..."

but it doesn't, so you deal with it. IE pretty much doesn't give errors.

Yes it does, you just have to learn how to understand them. And
sometimes, IE gives better (more useful) error messages than FF does.
Firefox ignores it as a keyword (at least in that context) and keeps on
going.

Then FF is wrong in that instance. 'class' is a reserved word and FF
should throw an error when it encounters it.
 
L

Lasse Reichstein Nielsen

Randy Webb said:
Then FF is wrong in that instance. 'class' is a reserved word and FF
should throw an error when it encounters it.

Javascript is allowed to delay failing until the erroneous code is
executed.
I would prefer Firefox to err early (a good principle), but it is allowed
to do as it does (ECMAS262 3rd ed, section 16).
/L
 
R

Randy Webb

Tony said the following on 6/14/2006 2:09 PM:
I haven't found that to be the case - perhaps I just haven't encountered
such a situation.

A lot of that depends on what you get used to. The first that comes to
mind is something like this:

<script type="text/javascript" src="ExternalJsFile.js"></script>
<script type="text/javascript">
someFunctionInExternalJsFile()
</script>

Now, assume you have the path wrong to the ExternalJsFile.js, or, you
have a typo in the name of the file.

IE error message:

Line: 2
Character: 1
Error: Invalid Character

FF error message:

Error: someFunctionIn is not defined

Neither exactly tells you what the problem is - that the external file
didn't get loaded. But IE never makes it to the function call. So which
is a better error message?

That is the first that comes to mind because I have seen it so many
times that when I see it I already know what is wrong without looking.

That doesn't mean I think IE always gives better error messages because
it doesn't. But FF doesn't always either. A better error message for the
above problem would be something like "File failed to load" or "File not
found". But both IE and FF's error message was useless.
 
R

Randy Webb

Tony said the following on 6/16/2006 1:40 PM:
Randy Webb wrote:


I'd say I agree on that point.

The error message for that code when run in Opera9 is:

Event thread: BeforeExternalScript
Linked script not loaded

Now *that* is a useful error message :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top