"reynard" <re*******@gmail.com> writes:
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
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'