468,110 Members | 1,893 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

changing doctype changes javascript behavior in FF?

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?

Jun 12 '06 #1
6 1989


reynard wrote:
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%27s_DOCTYPE_sniffing>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 12 '06 #2
"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.'
Jun 12 '06 #3
thanks for the explanation and the link. it's really helpful to know
that.

- reynard

Martin Honnen wrote:

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%27s_DOCTYPE_sniffing>

--

Martin Honnen
http://JavaScript.FAQTs.com/


Jun 12 '06 #4
reynard wrote:
I have a page that uses this doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
I'm not 100% certain, but I don't believe this is a valid doctype...
- yep, just checked: http://www.w3.org/QA/2002/04/valid-dtd-list.html#DTD
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.
More likely that your javascript's broken.
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;
(which is incorrect)
you have to do:
element.style.left = '25px';
(which is correct)
I thought it really was a weird behavior. Is there any other case where
changing the doctype changes js behavior?


javascript's behavior isn't changed, but the browser's response to the
CSS you're defining has changed - and you can expect that to happen in
other cases when you change doctype, especially if you aren't writing it
correctly in the first place.

Any particular reason not to use STRICT?
--
"The most convoluted explanation that fits all the available and made-up
facts is the most likely to be believed by conspiracy theorists"
Jun 12 '06 #5
Tony <to****@dslextreme.WHATISTHIS.com> writes:
reynard wrote:
I have a page that uses this doctype
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


I'm not 100% certain, but I don't believe this is a valid doctype...
- yep, just checked: http://www.w3.org/QA/2002/04/valid-dtd-list.html#DTD


It's valid. The system identifier (the URL) is optional.

/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.'
Jun 12 '06 #6
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
Tony <to****@dslextreme.WHATISTHIS.com> writes:
reynard wrote:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
I'm not 100% certain, but I don't believe this is a valid doctype...


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).
--
||| hexadecimal EBB
o-o decimal 3771
--oOo--( )--oOo-- octal 7273
205 goodbye binary 111010111011
Jun 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Greg Scharlemann | last post: by
2 posts views Thread by DartmanX | last post: by
17 posts views Thread by Rick Brandt | last post: by
1 post views Thread by Blasting Cap | last post: by
3 posts views Thread by John | last post: by
3 posts views Thread by Stephen Ward | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.