Javascript switch from quirksmode to standard

后端 未结 3 1913
庸人自扰
庸人自扰 2021-01-16 18:48

Is there a way to switch a page in quirksmode to standards mode using Javascript?

For example, assuming I have the following page with html in it. I\'ve tried the fo

相关标签:
3条回答
  • 2021-01-16 19:38

    The browser typically looks for a doctype at load time to determine whether to use "standards mode" or not, and loads/renders the page based on that decision. Even if you managed to insert a doctype declaration via script, it wouldn't be there in time to matter.

    The reason it works in an iframe is that the content is a new document, which is being "loaded" as you write it. Since the doctype is there, that content is rendered in standards mode.

    As mentioned in another question on this same topic, you might be able to get away with rewriting the whole document. Basically, (if the page is still in quirks mode,) get document.documentElement.innerHTML, and then write a new document with the proper doctype, and append the HTML from the original document. But it will cause a bunch of issues with scripts and such, and should be avoided. The ideal solution is to fix the HTML to include a doctype, rather than fudging it with a script.

    0 讨论(0)
  • 2021-01-16 19:44
    <script>
    document.open();
    document.write('<!doctype html><html></html>');
    document.close();
    </script>
    

    or any variant will not work, because a <SCRIPT> tag causes the HTML parser to create an HTML element and a HEAD element if none exists. By the time an HTML element exists (even an implicit one), a DOCTYPE on the token stream is ignored.

    Closing and reopening the document will just prevent parsing of any of the following HTML content.

    The only way to use document.write to output a DOCTYPE is to create a new blank document that you can write in.

    Just solve this server-side.

    0 讨论(0)
  • 2021-01-16 19:48

    You could try writing a X-UA-Compatible meta tag:

    <meta http-equiv="X-UA-Compatible" content="IE=9" />
    
    0 讨论(0)
提交回复
热议问题