Javascript switch from quirksmode to standard
Asked Answered
H

3

0

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 following.

<html><script src="js.js"></script></html>

and the following content in js.js

document.open();
document.write('<!doctype html><html></html>');
document.close();

From looking at the output above I'm basically trying to convert a page in quirksmode to standards mode, but it doesn't work. (setting the contents of a page like above on an iframe works fine)

iframe Works

It works you do document.createElement('iframe') append into the document and call iframe.contentWindow.document.write('<!doctype html>..'); on it. Unfortunately the same technique does not if I'm calling it on the current page.

Does anyone have a working fix switching a page in quirksmode to standards mode?

EDIT: Creating and appending an iframe and simply setting its height and width to 100%, works but isn't the goal here ;)

Hansom answered 12/8, 2011 at 22:53 Comment(10)
Who writes HTML without a doctype these days? And why can't you just edit the HTML to include one?Reedreedbird
@Ranyos yeah setting document.write doesn't work, but I find it funny that it works if you create an iframe. I wonder why?Hansom
@Reedreedbird that's dodging the original question ;)Hansom
@Hansom the content inside the iframe is in standards mode. Not the original pagePetticoat
@Lime, the iframe thing works inside the iframe only, the outer document will remain in quirksmode.Flyblown
@Lime: The original question isn't the one that needed to be asked. :)Reedreedbird
@Flyblown An iframe is supposed to be a window to another document, yet they behave differently. You can change the doctype of an iframe but not the current page.Hansom
possible duplicate of Need help with: jquery prepend doctype to htmlReedreedbird
@Reedreedbird the entire expressjs site is written without a doctype, not really sure why?Hansom
@Lime: First guess is, cause the devs aren't too bright. There are just too many issues with quirks mode, and every browser in common use will disable it if there's a doctype.Reedreedbird
R
3

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.

Reedreedbird answered 12/8, 2011 at 23:16 Comment(1)
Your (linked)solution worked! Thanks dude, could you either edit your current answer or post another one. I could close the question altogether, but you did theoritically answer it.Hansom
S
1
<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.

Scalariform answered 12/8, 2011 at 23:13 Comment(5)
If it actually got rid of the document and created a new one, how would your script run? Part of clearing the document would be to remove the script...Reedreedbird
It's what the mozilla docs says Thought they were trustworthy :| urg... Guess I have to make a new plan....Hansom
@Lime, reopening the document would halt HTML parsing preventing the rest of the HTML from being loaded into the new document. You would, at best, end up with a document in standard mode devoid of content.Scalariform
No content in standard mode is better then quirksmode :D at least for meHansom
@Lime, Then start your file with <script>document.write("<noscript>");</script> :)Scalariform
D
0

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

<meta http-equiv="X-UA-Compatible" content="IE=9" />
Duad answered 12/8, 2011 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.