How to get the "Document Mode" browser

I am developing some kind of JavaScript library. And I cause the problem that I have some specific problems for: Browser: IE8 / IE9 and document mode: IE7 I found a solution, but I do not want to use it in all situations and want to use it only when I have the above situation. I know that I can recognize the browser using:

return navigator.userAgent.toLowerCase().indexOf('MSIE 8') > -1; 

But I only recognize the browser version in this way, but not the document mode, and I do not want to use my solution when I have, for example, IE8 browser mode and IE 8 document mode. Is there a way to get page document mode in IE? Thanks in advance.

+8
javascript internet-explorer
source share
2 answers

You can use document.documentMode to specify exactly what the document mode IE uses.

This means that if you have IE9 browser mode set, but your document mode in IE8 will return document.documentMode == 8 (while the userAgent string will display as IE9). This is especially useful if your JS ever includes style changes, as it is a document mode that defines how IE displays the page, not browser mode. Compatibility mode really just changes the document mode (usually in IE7).

In the few cases that I need, I just used something like this to distinguish between IE:

 if (document.documentMode == 7) { doSomethingIn.IE7_only; } else { doSomething.everwhereElse; } 

Hope this helps.

+27
source share

I don't know how to get document mode 1, but it might be wise to solve the problem in a simpler way. Let's say you wanted to use document.querySelector in your scripts. This will not work in IE7 / IE7 mode. Thus, an additional test for the existence of document.querySelector itself would be a solution:

 return ~navigator.userAgent.toLowerCase().indexOf('MSIE 8') && document.querySelector; //=> IE8 and Docmode IE7 => false 

1 A method for checking document mode has been found: use document.documentMode . It returns an integer (like 7 for IE7 standards in document mode), 5 for Quirks mode . It will be undefined for non IE browsers.

+1
source share

All Articles