I'm off to a brilliant start in the javascript group. :|
I am not sure anyone ever gets of to "a good start" in this group. If you want to go on better
it would be a good idea to see if you can identify and read the group's FAQ, particularly the
material it contains (and refers to) on posting/quoting conventions.
Uhm, so I am bored and I took another look.
IE does not show an error until line 55 and it is
not the same error.
No there is a good reason for that.
And the OP is using document.write to print his menu.
And Randy is correct in stating that document.wirte does not work at all in XHTML DOMs, but that
is not a factor here as the page is served to Firefox browsers with a text/html content type
header so whatever the mark-up may resemble it is always being interpreted as tag soup HTML.
That is, even on browsers that support XHTML we or working with HTML here, and it is an HTML DOM
that is being scripted. The - document.write - calls will work here as programmed, the insanity
of using mark-up that resembles XHTML here is that in the event that the mark-up ever is
interpreted as XHTML by a browser the scripts on the page will immediately stop working.
here is the relevant part of the script.
if (typeof(loc)=="undefined" || loc==""){
var loc="";
var tt=document.body.innerHTML;
var ml=tt.match(/["']([^'"]*)orsclassnav.js["']/i);
if(ml && ml.length > 1) loc=ml[1];
}
}
document.write("<table border=\"0\" cellspacing=\"0\"
cellpadding= \"0\"><tr>");
tr(false); <--error here in FireFox
So my question, just to satisfy my own curiosity, what
is it that passes for IE here but fails for Firefox?
<snip>
Be careful what you wish for, as you may get it.
Javascript has a formal specification; ECMA 262, which defines the behaviour that is required of
implementations (which are then known as ECMAScript implementations). The specification defines
the syntax for the language, but it also allows for extensions, and for more tolerant
interpretations of the syntax (this means that an ECMAScript implementation must provide
everything specified by ECMA 262 but may provide more, and an implementation may elect to cope
with some ECMA 262 syntax errors as if they were not errors but it is not allowed to treat
anything that is valid by ECMA 262 as an error).
Disregarding most of the code on the page the problem boils down to code like:-
if(something){
tr();
function tr(){
// do something
}
}else{
tr();
function tr(){
// do something else.
}
}
- and in ECMAScript terms that is a syntax error. It should not be expected to work at all, so
it should not be at all surprising if when it does 'work' it does so inconsistently.
What appears above is an apparent attempt to conditionally create a - tr - function by placing
two function declarations in the branches of an - if-else - statement. It is a syntax error
because the two distinct syntax units from which an ECMAScript Program is constructed are
Statements and FunctionDeclarations, and as a result you cannot put a FunctionDeclarations
inside a Statement.
When javascript is executed FunctionDeclarations are acted upon prior to the execution of any
Statements in the pertinent execution context (i.e all declared functions will exist before any
of the code that can use them starts to be executed).
The ECMAScript implementation in IE browsers is JScript(tm) and it is tolerant of the above
syntax error. It ignores the fact that function declarations appear inside a statement and just
treats them as normal function declarations. It creates the corresponding function objects
before any other code is executed. So JScript treats the code above as if it was:-
function tr(){
// do something
}
function tr(){
// do something else.
}
if(something){
tr();
}else{
tr();
}
- and because there are two declarations for the same function two function objects get created
but the second one effectively replaces the first and becomes the one that defines - tr -.
Here executing - tr - is not a problem because the functions have been created before the call
is executed. Though the functions have not been created conditionally so the actual - tr - used
may coincidentally be the wrong one for the IE.
The ECMAScript implementation in Firefox is JavaScript(tm), and it has an extension (an extra
facility) that is a FunctionStatement. A FunctionStatement looks just like a FunctionDeclaration
but being a statement it is allowed to appear inside a BlockStatment. So when JavaScript(tm)
sees what would otherwise appear to be a FunctionDeclaration in a context where only a Statement
would be allowed it interprets that code as a FunctionStatement.
FunctionStatements are evaluated (and the corresponding function objects created) when execution
gets to the statement in the code. So in Firefox the original code gets to the execution of
the - tr - function before it has evaluated the FunctionStatements and so it is trying to
execute the function before the function has been created. Hence the error on Firefox.
Richard.