changing doctype changes javascript behavior in FF?

Discussion in 'Javascript' started by reynard, Jun 12, 2006.

  1. reynard

    reynard Guest

    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?
     
    reynard, Jun 12, 2006
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Jun 12, 2006
    #2
    1. Advertising

  3. "reynard" <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 12, 2006
    #3
  4. reynard

    reynard Guest

    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/
     
    reynard, Jun 12, 2006
    #4
  5. Tony <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 12, 2006
    #5
  6. Lasse Reichstein Nielsen <> writes:

    > Tony <> 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
     
    Eric B. Bednarz, Jun 13, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmFtZXM=?=
    Replies:
    3
    Views:
    5,740
    =?Utf-8?B?U2NvdHQgU2ltb25z?=
    Oct 1, 2004
  2. Jake

    Changing DOCTYPE in template

    Jake, Aug 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    397
  3. Jeff
    Replies:
    9
    Views:
    574
    A-OK-SITE
    Jan 27, 2008
  4. DartmanX

    Dynamically Changing the DOCTYPE

    DartmanX, Oct 17, 2005, in forum: Javascript
    Replies:
    2
    Views:
    107
    Martin Honnen
    Oct 17, 2005
  5. Larry Lindstrom
    Replies:
    19
    Views:
    1,308
    Jonathan N. Little
    Jun 12, 2012
Loading...

Share This Page