changing doctype changes javascript behavior in FF?

R

reynard

I have a page that uses this doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

but when I change to this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

it breaks my javascript. Particularly I have a code that try to set
element.style.left, when I use the second doctype, FF complains "Error
in parsing value for property 'left'. Declaration dropped"
I turns out that I was setting it to just integer number before, and
when I use the second doctype you have to set it with the unit as well.

So instead of:
element.style.left = 25;

you have to do:
element.style.left = '25px';

I thought it really was a weird behavior. Is there any other case where
changing the doctype changes js behavior?
 
M

Martin Honnen

reynard said:
So instead of:
element.style.left = 25;

you have to do:
element.style.left = '25px';

I thought it really was a weird behavior.

IE5/Mac started that doctype sniffing and working in so called quirks
mode versus standard compliants mode or strict mode depending on the
doctype. By now Mozilla, Opera and IE 6/Win all do that sniffing and
have different rendering modes.
CSS requires you to have a number plus a unit for the left property so
if you use a doctype that puts Mozilla in standards compliant mode
Mozilla's CSS parser ignores values which are not complying with the CSS
specification (both in static CSS stylesheets as well as when script
manipulate CSS values).
See <http://developer.mozilla.org/en/docs/Mozilla's_DOCTYPE_sniffing>
 
L

Lasse Reichstein Nielsen

reynard said:
I have a page that uses this doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

but when I change to this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

it breaks my javascript.

That change most likely causes the browser to switch to standards
compliant mode. If that breaks your script, I'd say the script was
already broken by relying on non-standard browser behavior.
I turns out that I was setting it to just integer number before, and
when I use the second doctype you have to set it with the unit as well.

So instead of:
element.style.left = 25;

you have to do:
element.style.left = '25px';

It's not really javascript that is changed here, but CSS
interpretation. The first assignment converts 25 to a string and
assigns it to the "left" CSS property. That is not a valid CSS
value, but in "quirks" mode, the browser chooses to accept it anyway,
with a default unit of "px" applied.

In standards comliant mode, the incorrect CSS value is, correctly,
rejected.
I thought it really was a weird behavior. Is there any other case where
changing the doctype changes js behavior?

There are a few. My two first thoughts when I read the first sentence
of your post was missing CSS units and document.documentElement.

In standards mode, the root of the document is document.documentElement,
whereas in quirks mode, that element might be missing, and document.body
is the root element of all displayed elements.

You can read more about standards/quirks mode and the effect of
changing the DOCTYPE in the links here:
<URL:http://www.infimum.dk/HTML/references.html#ref_1_6>

/L
 
R

reynard

thanks for the explanation and the link. it's really helpful to know
that.

- reynard
 
E

Eric B. Bednarz

Assuming the reference concrete syntax, what would _not_ be *valid*
about it? (Even if the formal public identifier were unknown, a
system's catalog could be set up to know better, and otherwise the
document instance set would be invalid, not the document type
declaration.)
It's valid. The system identifier (the URL) is optional.

For conforming SGML applications, yes (and so is the FPI). HTML 4.01
isn't one, as section 7.2 implicitly proclaims, and it is fair enough to
mention (even in a javascript group :) that user agents don't treat it
as one either but invite for voodoo feature switching instead (which
actually appears to be the 'problem' here, but *that* is not about
client-side scripting indeed).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top