IE11 detect whether compatibility view is ON via javascript - javascript

does anyone know how to check if IE 11 compatibility mode is ON when I'm on a website through javascript?
I added the url to the list compatibility view settings. But when I do
navigator.userAgent
in developer tools, it returns
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C;
.NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729;
InfoPath.3; rv:11.0) like Gecko
Looking at the microsoft website (http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx), it says
The compatible ("compatible") and browser ("MSIE") tokens have been
removed.
Any help on detecting whether a page is using compatibility view via javascript would be really helpful. Thanks in advance.

SOLVED
While searching for an answer to this question myself, I found this solution from Nenad Bulatovic in another thread but his response wasn't marked as the correct answer. I tested this out in IE11 and downgrading to IE5 and found that it works for IE7-IE11, which is great. I wanted to share it here in case anyone else finds it useful.
iecheck.js
function trueOrFalse() {
return true;
}
function IeVersion() {
//Set defaults
var value = {
IsIE: false,
TrueVersion: 0,
ActingVersion: 0,
CompatibilityMode: false
};
//Try to find the Trident version number
var trident = navigator.userAgent.match(/Trident\/(\d+)/);
if (trident) {
value.IsIE = true;
//Convert from the Trident version number to the IE version number
value.TrueVersion = parseInt(trident[1], 10) + 4;
}
//Try to find the MSIE number
var msie = navigator.userAgent.match(/MSIE (\d+)/);
if (msie) {
value.IsIE = true;
//Find the IE version number from the user agent string
value.ActingVersion = parseInt(msie[1]);
} else {
//Must be IE 11 in "edge" mode
value.ActingVersion = value.TrueVersion;
}
//If we have both a Trident and MSIE version number, see if they're different
if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) {
//In compatibility mode if the trident number doesn't match up with the MSIE number
value.CompatibilityMode = value.TrueVersion != value.ActingVersion;
}
return value;
}
iecheck.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing IE Compatibility Mode</title>
<script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">
var ie = IeVersion();
document.write("IsIE: " + ie.IsIE + "</br>");
document.write("TrueVersion: " + ie.TrueVersion + "</br>");
document.write("ActingVersion: " + ie.ActingVersion + "</br>");
document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>");
</script>
</body>
</html>
source: Detect IE10 compatibility mode

I wrote a JavaScript function, ie-truth, to do just this. How it works is that in IE 11 if compatibility mode is turned on, the User Agent string will contain the Trident version number for IE 11 (7.0) as well as the MSIE version number for an older version of IE (such as 7.0 for IE 7). This also applies to compatibility mode in older versions of IE.

Add this to web.config file and application will overwrite user's setting.
<configuration>
...
<system.webServer>
...
<httpProtocol>
<customHeaders>
<clear/>
<add name="X-UA-Compatible" value="IE=8; IE=9; IE=EDGE" />
</customHeaders>
</httpProtocol>
...
</system.webServer>
...
</configuration>
Place the "system.webServer" tag at the end of your web.config file just before the closing "configuration" tag. Additionally, you can add the X-UA-Compatible tag in IIS on your webserver by selecting your website and clicking on the HTTP Response Headers icon.

A reliable solution you can edit that works for Internet Explorer 8 to 11 (needs extra code to support < IE8).
Nasty side-effects (only if IE < 11 or documentMode < 11 -- ouch):
This will cause problems with //# sourceMappingURL=xx.js (e.g. in jQuery < 1.11, or other libraries that haven't updated to the newer //# format).
Apparently #cc_on dramatically slows down js evaluation (re Paul Irish who knows his stuff).
The basic reason it works is that:
var ieRealVersion = Function('return /*#cc_on #_jscript_version#*/;')();
returns the real version of IE regardless of compatibility mode. Notes:
ieRealVersion is undefined for IE11 in IE11 mode, but document.documentMode == 11 in that case.
ieRealVersion is 5.8 for IE8.
ieRealVersion is 5.7 for IE7, and also 5.7 for IE6 SP3. However documentMode is undefined so you need extra code to detect actual browser (fairly trivial, but I don't support IE6/7 any more, so I haven't bothered to code that).
This is a reliable way to sniff for Internet Explorer 6-10 that I have used for many years with no problems (but which has caused problems for others due to #cc_on).

I would recommend that one uses feature detection rather than indirectly querying the browser version. So, for example, if you require the HTML 5 history API feature, do something like:
if (window.history && window.history.pushState) {
console.log('This is a SUPPORTED browser');
} else {
console.log('NO! You require a browser that does X, please try installing ...');
}

Per the user agent article of the IE Compatibility Cookbook, there is a way you can tell that compat mode is enabled, but only when the user-agent string cooperates.
Specifically, if the browser token says MSIE 7.0 and the Trident token says Trident/7.0, that's a pretty clear indication. Still, the changes to the UA string since IE11 RTM show that you cannot--and should not--rely on it as a predicable resource in future versions.
To learn more about the individual tokens, see the Understanding user-agent strings topic. (It's not entirely current, but what is there seems relevant to your interests.)
Hope this helps...
-- Lance

A simple solution, try it in the console:
if (typeof(window.getSelection) == "undefined") {
alert("Unsupported browser!");
} else {
alert("Supported browser");
}
Will detect any IE less than IE9 (including compatibility view).
Would also detect Chrome 4-14 according to caniuse.
IE 6-8: Supports selection events, but not window.getSelection()
Ref: https://caniuse.com/#feat=selection-api

Related

JavaScript code detects IE 10 as supported version instead of as out of date

The code below generates alert "Your browser is supported" even if the version is IE 10. It works perfectly with browsers such as IE 9, 8 and 7. I need to ensure only version IE 11 is allowed. Is there really anything wrong with the code?
<script type="text/javascript">
jQuery(document).ready(function($){
// true on IE11
// false on Edge and other IEs/browsers. filters.hide();
// Allow only IE 11 and other browsers like Chrome, Safar, firefox so on,
// but never allow lower versions of IE starting IE 10.
var div = document.createElement("div");
div.innerHTML = "<!--[if lt IE 11]><i></i><![endif]-->";
var isIeLessThan11 = (div.getElementsByTagName("i").length == 1);
if (isIeLessThan11) {
alert("Your web browser is out of date. Please complete this form using an approved browser, such as Edge, Chrome, Safari, or Firefox.");
} else {
alert("Your browser is supported");
}
});
</script>
According to wikipedia, conditional comments are not supported in IE10 and above. https://en.m.wikipedia.org/wiki/Conditional_comment

Detect if any kind of IE (MSIE) [duplicate]

This question already has answers here:
Internet Explorer 11 detection
(12 answers)
Closed 8 years ago.
I dont want to allow users to access my site with Microsoft Internetexplorer (ANY VERSION).
What I´ve found so far was to detect if it´s lower or equal version 10.
A very annoing thing: Internetexplorer >v10 doesn´t admit to be a InternetExplorer.
What i´ve found and tried so far:
if(navigator.appVersion.indexOf("MSIE")!=-1){
alert("You use IE. That´s no good.");
}
or
if ( $.browser.msie ) {
alert( $.browser.version );
}
and
http://msdn.microsoft.com/en-us/library/ie/ms537509%28v=vs.85%29.aspx
I would use any solution if it is in javascript, jquery or php if there is one.
This works for me to detect any Version of the IE 5-11 (Internet Explorer) (Aug/05/2014):
if (navigator.appName == 'Microsoft Internet Explorer' || !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1))
{
alert("Please dont use IE.");
}
This is because each release of Internet Explorer updates the user-agent string.
MSIE tokens have been removed in Internet Explorer 11 and $.browser uses navigator.userAgent to determine the platform and it is removed in jQuery 1.9.
You can use following code to determine the browser with pure java-script.
var isIE = !!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g);
if(isIE){
alert("IE");
}
else{
alert("Not IE");
}
Thanks!
if you are not interessted wich version of ie the user currently use you can try get it work with detecting if the browser supports the Conditional Compilation Statements
http://msdn.microsoft.com/en-us/library/7kx09ct1%28v=vs.80%29.aspx
if(/*#cc_on!#*/false)
{
// You use IE. That´s no good.
alert("oh my god");
}
You can use conditional compilation
, e.g.
<script>
var isIE = false;
/*#cc_on isIE = true; #*/
</script>
But note that IE11 doesn't observe this in Standards Mode. User Agent sniffing is generally a bad idea, but as IE becomes more standards-compliant, it also becomes harder to detect (hopefully also meaning less need to)
For IE> 10 which is currently IE 11, user-agent carries something in Browser's HTTP request headers
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
You can put a check on "rv:11.0" for version 11. Let me know if you need code for this.
I've found (maybe in SO) in the past this script and it worked for me (IE 10 too)
<![if IE]>
<script type='text/javascript'>
if(/*#cc_on!#*/false)
var bIsIE = 1;
</script>
<![endif]>
and then
if (typeof (bIsIE) != 'undefined')
{
//IE :(
}
else
{
//NOT IE :)
}

Device browser detection

I have built a parallax intro for a clients site - due to the limited budget the animation will only work on higher end browsers, IOS and ie9.
Therefore I need to create a detection script in the sites homepage which will detect the following
IF:
ie9/ firefox / chrome/ safari - stay on current site
IOS - Go to IOS version
Android - Skip to main site
IE8 and below - skip to main site
I have carried out 'is mobile' detections in the past with PHP - but the above is pretty specific so I'm not sure how to approach it.. The main site is aspx, so I could make the animation page into a aspx page also and use server side detection, or look at Javascript/jquery options or plugins - or a combination of both..?
Can anyone recommend a good solution?
In hopes of not getting into browser detection / feauture detection argument blah blah blah, http://www.quirksmode.org/js/detect.html has a good script to handle this
Try with following code, uses the navigator object::
var ua = navigator.userAgent;
if(navigator.appName == "Netscape"){ //for Firefox, Safari and Chrome
//do nothing, stay on this page.
return;
}
else if(navigator.appName == 'Microsoft Internet Explorer'){
//check for version
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null){
version = parseFloat( RegExp.$1 );
}
if(version >= 9.0){
//do nothing, stay on this page.
return;
}
else{
//redirect to the site for lower IE versions.
}
}
else if(ua.match(/Android/i)){
//code for skipping to Android version
}
else if(ua.match(/iPhone/i)){
//code for skipping to iPhone version
}
else if(ua.match(/iPad/i)){
//code for skipping to iPad version
}
there is a JQuery object $.browser which could give you what you need in javascript here is the api call.
On the Server Side there is a .net Request.Browser object also here is the MSDN Api for it.

IF IE neglect/hide div

I have page like this
<html>
<head>
<div id="container">
Hello, this is NON-IE content.
</div>
</head>
</html>
What I want is to Stop Internet Explorer [ IE ] from loading OR Neglecting <div id="container"> & allow all other browsers to load It.
Don't want to use <!--[if IE 7]>.
Looking for Javascript to work with.
How Can I achieve this ?
Thanks
Mandar
For users that don't want to use jQuery, you can simply do:
if (/*#cc_on!#*/false) {
alert('This is Internet Explorer!');
}
http://devoracles.com/the-best-method-to-check-for-internet-explorer-in-javascript
What we see up there? I declared a new
variable, called IE, which has the
value a comment block followed by
‘false‘. The above variable will be
understood by IE: var IE = !false,
because Internet Explorer uses JScript
— a Javascript-like dialect of the
standard ECMAScript — instead of
Javascript which is used by all the
other browsers. JScript can parse the
comments, just like Internet Explorer
(see conditional HTML comments post).
This is a unique feature of IE, none
of the other browsers can do it, so
Firefox, Chrome, Safari, Opera, all
will understand the above declaration
as IE = false.
Note: If any other browser were to use "JScript" this would pass, but since JScript is written by Microsoft I think you're safe. Another method is the navigator object, which you can pull the application name. Although some applications like to spoof this, so I believe the JScript is a bit more reliable.
if (navigator.appName == 'Microsoft Internet Explorer') {
alert('This is Internet Explorer!');
}
Edit: This was more to help users in detecting IE, not about directly answering the users question. Also, for users not wanting to use jQuery you could simple do document.getElementById('container').style.display = 'none'; -- Just figured I'd add this in since my post did mention "without using jQuery".
jquery makes this easy:
if ( $.browser.msie ) {
$("#container").css("display","none");
}
document.all
A very dirty option is using document.all which IE supports.
if(document.all){
document.getElementById("container").style.display = "none";
}
For some crazy reason, Chrome does support document.all, but checking for document.all returns false in it.
navigator
Another option is to look at the navigator object.
if (navigator && navigator.appName == 'Microsoft Internet Explorer'){
document.getElementById("container").style.display = "none";
}
This could fail on browsers that spoof themselves as other browsers.
comparison 'feature'
You could also use a simple comparison.
if('\v' == 'v'){
document.getElementById("container").style.display = "none";
}
I personally would not do this, but it is a neat detection to list.
Try:
if($.browser.msie) $("#container").remove();

What is the correct way to detect Opera using jQuery?

Amazon.com recently updated their javascript, and it's causing problems with some Opera browsers.
Their browser detection code looks like so, but it's faulty:
function sitbReaderIsCompatibleBrowser() {
if (typeof(jQuery) == 'undefined') {
return false;
} else {
var version = jQuery.browser.version || "0";
var splitVersion = version.split('.');
return (
(jQuery.browser.msie && splitVersion[0] >= 6) // IE 6 and higher
|| (jQuery.browser.mozilla && (
(splitVersion[0] == 1 && splitVersion[1] >= 8) // Firefox 2 and higher
|| (splitVersion[0] >= 2)
))
|| (jQuery.browser.safari && splitVersion[0] >= 500) // Safari 5 and higher
|| (jQuery.browser.opera && splitVersion[0] >= 9) // Opera 5 and higher
);
}
}
Nothing obviously wrong jumps out at me with this code, but I've never used jQuery before so I don't know.
Even though this code looks like it's attempting to let Opera users through, when I visit the page with Opera 9.64 I get an "unsupported browser" message. If I change Opera's settings to report itself as Firefox, the page works perfectly! With that in mind, I'm pretty sure it's a problem with the script and not the browser.
Any jQuery experts have a suggestion?
You can replicate the behavior by visiting any book on Amazon and clicking the "look inside this book" link.
Prior to jQuery 1.3, you could use jQuery.browser:
if( $.browser.opera ){
alert( "You're using Opera version "+$.browser.version+"!" );
}
From version 1.3, you should use jQuery.support instead.
Main reason for this is that should should avoid checking for browsers, as features may change from version to version, making your code obsolete in no time.
You should always try to use feature detection instead. This will allow you to see if current browser supports the feature you're trying to use, regardless the browser brand, version, etc.
There is a special window.opera object which is present in all Opera 5+ browsers. So something as simple as:
if (window.opera && window.opera.buildNumber) {
// we are in Opera
}
would be enough.
I check for Opera like this:
if (/Opera/.test (navigator.userAgent)) // do something
Why would you want jQuery?
It is much better to detect javascript capabilities rather than browser userAgent.
ie DOM, XmlHttpRequest, eventing model (event.target vs event.srcElement), ActiveX, Java etc
By focusing on the API functions that you will require, rather than a target browser you will create a more robust set of scripts, and inevitably less special casing.
This link here at opera will probably tell you more
A very simple way from Opera themselves:
if (window.opera) {
//this browser is Opera
}
Source: http://my.opera.com/community/openweb/idopera/
The main reason why Amazon fails on Opera is because the send different code from the server side already... If you visit the same page with Firefox and then save that page and reopen it in Opera it works fine...
But they promised to fix that sometime in January...
I think this way is the best
if ( window.opera.version() == 12) {
}
This example check if opera version is 12. Very useful when I have problems with font-face in Opera.
I don't know for sure ( i never really check for opera anyway) but if the built-in jQuery functionality doesn't detect opera, may be a bug with the jQuery which needs to be fixed. I would suspect if that's the case, it should get resolved fairly quickly.
In current HTML5 times, you can also check for browser features instead often.
if (!window.FormData) { alert("xmlhttprequest L2 FormData interface not available"); }

Categories

Resources