Skip to content Skip to sidebar Skip to footer

Need Help With: Jquery Prepend Doctype To Html

Here's my situation: I am editing an application's CSS style sheet. I can ONLY edit the CSS style sheet (unless I can creatively glom onto another file using the CSS, or possibly

Solution 1:

It's not <doctype html>. It's:

<!DOCTYPE html><html (xmlnsoranyotherattributesyouwant)>

<!DOCTYPE is not an element. It has <! at the start, which is invalid for an element. This is the the “doctype declaration”, and it cannot usefully be altered after initial parsing.

Even on browsers whose DOM interfaces let you move/replace the DocumentType node representing the doctype declaration, this does not have the effect of changing between Quirks and Standards mode, which is something that is decided only at initial load time. You cannot mutate a document between modes.

You can load a new document from the existing document but with a changed mode:

<!-- no doctype, loads in Quirks Mode (BackCompat) --><html><!-- rest of the document, then at the end: --><script>
        alert('now in compatMode '+document.compatMode);
        if (document.compatMode==='BackCompat') {
            setTimeout(function() {
                var markup= document.documentElement.innerHTML;
                markup= '<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"lang="en"xml:lang="en">'+markup+'</html>';
                document.open();
                document.write(markup);
                document.close();
            }, 0);
        }
    </script></html>

But I would strongly advise against it. It's ugly, will reset any state and redraw at the end of load time, and has all sorts of negative implications for scripting.

If you want Standards Mode, you really need to be adding the doctype to the HTML itself. If you absolutely can't touch the application, how about using an ISAPI filter (assuming your web server is IIS) to add the doctype to its HTML output?

Post a Comment for "Need Help With: Jquery Prepend Doctype To Html"