Javascript从quirksmode转换为standard [英] Javascript switch from quirksmode to standard

查看:34
本文介绍了Javascript从quirksmode转换为standard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用Javascript将页面从quirks模式切换到标准模式?

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

例如,假设我在下面的页面中有html.我尝试了以下方法.

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

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

以及js.js中的以下内容

and the following content in js.js

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

从上面的输出看,我基本上是在尝试将quirksmode中的页面转换为standard模式,但这是行不通的.(在iframe上像上面一样设置页面的内容就可以了)

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)

您可以将 document.createElement('iframe')附加到文档中并调用 iframe.contentWindow.document.write('<!doctype html> ..'); 就可以了.不幸的是,如果我在当前页面上调用相同的技术,则不会.

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.

编辑:创建并附加iframe并将其高度和宽度简单设置为100%即可,但这不是目标;)

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

推荐答案

浏览器通常会在加载时查找文档类型 ,以确定是否使用标准模式",然后进行加载/渲染基于该决定的页面.即使您设法通过脚本插入doctype声明,它也不会及时出现.

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.

它在iframe中起作用的原因是内容是一个新文档,在您编写该文档时正在加载"该文档.由于存在doctype,因此该内容以标准模式呈现.

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.

有关同一主题的另一个问题所述,您可能可以摆脱重写整个文档的麻烦.基本上,(如果页面仍处于怪癖模式下)获取 document.documentElement.innerHTML ,然后编写具有适当doctype的新文档,并从原始文档中附加HTML.但这会导致脚本等问题,因此应避免使用.理想的解决方案是修复HTML以包含doctype,而不是使用脚本对其进行篡改.

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.

这篇关于Javascript从quirksmode转换为standard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆