Html 如何判断浏览器是否处于“怪癖”模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/627097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell if a browser is in "quirks" mode?
提问by Joel Coehoorn
Let's suppose you have a page with a relatively strict doctype and HTML markup that's pretty close to compliant, but perhaps misses in a few silly ways, perhaps because of user content that's out of your control... say you're working on a content management system or a theme for a content management system where you control some basic structure and need some javascript, but you're not responsible for everything else that goes into pages.
假设您有一个具有相对严格的文档类型和 HTML 标记的页面,该页面非常接近合规性,但可能会以一些愚蠢的方式遗漏,可能是因为用户内容不受您的控制......假设您正在处理一个内容管理系统或内容管理系统的主题,您可以在其中控制一些基本结构并需要一些 javascript,但您不对进入页面的所有其他内容负责。
How can you tell (or: what will determine) when the browser decides to go into "quirks" mode rather than use it's more standards compliant engine?
当浏览器决定进入“怪癖”模式而不是使用它更符合标准的引擎时,你怎么知道(或:什么将决定)?
I'm looking for answers for each of the major browsers, since IE, Chrome, Safari, and Firefox will of course all handle that differently. Is one single error enough to force it or do you have some leeway?
我正在为每个主要浏览器寻找答案,因为 IE、Chrome、Safari 和 Firefox 当然都会以不同的方式处理这些问题。一个错误是否足以迫使它或您有一些回旋余地?
回答by Chris Ballance
In Firefox and Operayou can determine if your browser is in "quirks mode" by checking page info.
在Firefox 和 Opera 中,您可以通过检查页面信息来确定您的浏览器是否处于“怪癖模式”。
Using document.compatMode
, will tell you the mode you are in with most browsers.
使用document.compatMode
, 将告诉您大多数浏览器所处的模式。
In Chrome, Safari, and IE, run this javascript in the address bar:
在Chrome、Safari 和 IE 中,在地址栏中运行此 javascript:
javascript:window.alert('You are in ' + (document.compatMode==='CSS1Compat'?'Standards':'Quirks') + ' mode.')
(note that you'll need to re-type the javascript:
portion after pasting into your address bar, due to recent security changes)
(请注意javascript:
,由于最近的安全更改,您需要在粘贴到地址栏中后重新键入该部分)
回答by Dave Webb
As you can query the render mode in JavaScript you can have a Bookmarkletwhich will tell you which render mode a page is using.
正如您可以在 JavaScript 中查询渲染模式一样,您可以拥有一个Bookmarklet,它会告诉您页面正在使用哪种渲染模式。
I found this render mode bookmarkletwhich works well for me:
我发现这个渲染模式书签对我来说效果很好:
javascript:m=(document.compatMode=='CSS1Compat')?'Standards':'Quirks';window.alert('You%20are%20in%20'%20+%20m%20+%20'%20mode.');
回答by Steerpike
The full answer to your actual specific question of 'Is one single error enough to force it or do you have some leeway?' is that it totally depends on the error. For example,
对您的实际特定问题“一个错误是否足以迫使它或您有一些回旋余地?”的完整答案。是它完全取决于错误。例如,
<!-- Comment -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
will force quirks mode in IE 6 & 7 despite not really being an error (they just throw a total wobbly when the very first line of the file is not a declaration). A quick list of types/quirks can be found here
尽管不是真正的错误,但仍将在 IE 6 和 7 中强制使用 quirks 模式(当文件的第一行不是声明时,它们只会抛出完全不稳定的情况)。可以在此处找到类型/怪癖的快速列表
Try sticking the following line in your HTML for testing (very bad javascript behavious I'm passing on here - sorry...make sure this never goes live :)
尝试在您的 HTML 中粘贴以下行进行测试(非常糟糕的 javascript 行为,我在这里传递 - 抱歉......确保这永远不会上线:)
<a href="javascript:alert(document.compatMode);">What mode am I?</a>
回答by Sébastien Nussbaumer
According to http://www.quirksmode.org/css/quirksmode.html: "The problem was that some pages written in quirks mode did have doctypes. Therefore each browser has its own list with doctypes that trigger quirks mode. See this browser comparison chart for an overview of these lists : http://hsivonen.iki.fi/doctype/"
根据http://www.quirksmode.org/css/quirksmode.html:“问题是一些以 quirks 模式编写的页面确实有文档类型。因此,每个浏览器都有自己的列表,其中包含触发 quirks 模式的文档类型。请参阅此浏览器这些列表概述的比较图表:http: //hsivonen.iki.fi/doctype/”
Hope this helps
希望这可以帮助
回答by i_am_jorf
If you tell IE that it should be strict (via doctype) it will not change its mind halfway through the page.
如果你告诉 IE 它应该是严格的(通过 doctype),它不会在页面中途改变主意。
回答by Bill the Lizard
If I understand quirks mode correctly, a page that does not validate against its declared doctype is not enough to trigger quirks mode. It just won't display correctly.
如果我正确理解了 quirks 模式,则未根据其声明的 doctype 进行验证的页面不足以触发 quirks 模式。它只是不会正确显示。
The best resource I've found for determining how different browsers handle each doctype is here.
我找到的用于确定不同浏览器如何处理每个文档类型的最佳资源是这里。
回答by Arieleo
For Firefox with Web Developer Toolbar add on, you can look at the trio of icons on the right of the bar. The leftmost one tells you what mode you are in.
对于带有 Web Developer Toolbar 插件的 Firefox,您可以查看工具栏右侧的三个图标。最左边的一个告诉你你处于什么模式。
回答by Ronen Festinger
In IE you will see it in the developer tools (pressing F12), it says it in the menu: Document Mode:... And you can also force a different mode there.
在 IE 中,您将在开发人员工具中看到它(按 F12),它在菜单中说:文档模式:...您也可以在那里强制使用不同的模式。
回答by Miro
in html5 page, write "<!DOCTYPE html>
" start with page can change to
document.compatMode='CSS1Compat'
在html5页面中,写“ <!DOCTYPE html>
”开头的页面可以改成document.compatMode='CSS1Compat'