Bastian Perez
Bastian Perez

Reputation: 13

Javascript and Doctype in firefox bug

i have this javascript code :

function pageWidth() {
    return (window.innerWidth != null
        ? window.innerWidth
        : (document.body != null
            ? document.body.offsetWidth
            : null
    ));
}

function bodyloaded() {
    winWidth = pageWidth();
    window.scroll(0, 0);
    scrAmount = Math.floor(
        ((document.body['scrollWidth'] - document.body.offsetWidth)/2) + 8
    );
    scrollBy(scrAmount, 0);
}

and is applied on body tags in methods onload and onresize , the problem is if i put any doctype , this code doesnt work on firefox but works on IE. I debug the value of scrollWidth and offsetWidth , and allways get the same value , this happend using a Doctype.

Any solution?

Upvotes: 1

Views: 308

Answers (1)

Boris Zbarsky
Boris Zbarsky

Reputation: 35074

In quirks mode (no doctype or a quirks mode doctype), document.body.scrollWidth actually returns the scrollWidth of the document instead of the body in some cases. In standards mode (with most doctypes), it returns the scrollWidth of the body, and document.documentElement.scrollWidth returns the scrollWidth of the document in some cases. See http://dev.w3.org/csswg/cssom-view/#dom-element-scrollwidth for the spec draft for this.

Upvotes: 3

Related Questions