Need help with: jquery prepend doctype to html

天涯浪子 提交于 2019-11-28 14:03:23

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

<!DOCTYPE html>
<html (xmlns or any other attributes you want)>

<!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><html xmlns="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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!