11

I am developing some kind of JavaScript library. And i cause the problem that i have some specific issues for: Browser : IE8 / IE9 and Document Mode : IE7 I found the solution, but i don't want to use it in all situation, and want to use it just when i have the situation described above. I know that I can recognize browser by using:

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

But i recognize just browser version in such way but not the document mode, and i don't want to use my solution when I have, for example, browser mode IE8 and document mode IE 8. Is there a way to get page document mode in IE? Thanks in advance.

Mikhail
  • 759
  • 2
  • 9
  • 26

2 Answers2

30

You can use document.documentMode to return exactly what document mode IE is using.

Meaning that if you have your browser mode set to IE9, but your document mode to IE8 it will return document.documentMode == 8 (while the userAgent string will still show up as IE9). This is particularly useful if your JS ever includes styling changes as it is the document mode that determines how IE renders a page, not the browser mode. Compatibility mode really just changes the document mode (usually to IE7).

In the few cases I've needed to I've just used something like this to differentiate IE's:

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

Hope that helps some.

Sam Thornton
  • 953
  • 7
  • 18
  • Can I set document mode.like suppose if (document.documentMode == 8) { set document.documentmode to any value} – choudhury smrutiranjan parida Apr 07 '14 at 11:36
  • Unfortunately you can't set it with javascript. But you can use the x-ua-compatability meta tag in order to specify a desired IE mode. Note that it'll on work on legacy versions of IE (e.g. not IE11+). Read this for some more info: http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx – Sam Thornton Apr 08 '14 at 22:12
  • 3
    Note that `document.documentMode` is only supported in IE8+, so you'll need additional 'feature-detection' if you need to target native IE7 rendering. – WynandB Jul 29 '14 at 02:21
1

I don't know how to retrieve the document mode1, but it may be wise to address the problem in a more basic way. Let's say you wanted to use document.querySelector in your scripting. That would fail in IE8/document mode IE7 Standards. So an additional check for the existence of document.querySelector itself would be the solution:

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

1 Found a way to check for document mode: use document.documentMode. It returns an integer (so 7 for document mode IE7 standards), 5 for Quirks mode. It will be undefined for non IE browsers.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Yes thx it is good idea, but I don't have fail of javascript or some missed function I just have some different behavior of the same functions in different document mode. As another way to use your solution maybe you know some function which willn't work under "DocumentMode IE7"? If I find such function I can use it to recognize document mode. – Mikhail Oct 29 '12 at 12:27
  • Well, `document.querySelector` doesn't work in docmode IE7 ;). Anyway, found a helpfull IE-document property, see the updated answer. – KooiInc Oct 29 '12 at 12:32
  • Note that document.documentMode is only supported in IE8+, so you'll need additional 'feature-detection' if you need to target native IE7 rendering. – WynandB Jul 29 '14 at 02:21