I am calling a function like the one below by click on divs with a certain class.
Is there a way I can check when starting the function if a user is using Internet
I've placed this code in the document ready function and it only triggers in internet explorer. Tested in Internet Explorer 11.
var ua = window.navigator.userAgent;
ms_ie = /MSIE|Trident/.test(ua);
if ( ms_ie ) {
//Do internet explorer exclusive behaviour here
}
If all you want to know is if the browser is IE or not, you can do this:
var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');
if ((old_ie > -1) || (new_ie > -1)) {
isIE = true;
}
if ( isIE ) {
//IE specific code goes here
}
I recommend this now. It is still very readable and is far less code :)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
Thanks to JohnnyFun in the comments for the shortened answer :)
Firstly, if you can, you should use @supports
statements instead of JS for checking if a browser supports a certain CSS feature.
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(Note that IE doesn't support @supports
at all and will ignore any styles placed inside an @supports
statement.)
If the issue can't be resolved with @supports
then you can do this:
// JS
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
document.documentElement.classList.add('ie')
}
/* CSS */
.element {
/* styles that apply everywhere */
}
.ie .element {
/* styles that only apply in IE */
}
(Note: classList
is relatively new to JS and I think, out of the IE browsers, it only works in IE11. Possibly also IE10.)
If you are using SCSS (Sass) in your project, this can be simplified to:
/* SCSS (Sass) */
.element {
/* styles that apply everywhere */
.ie & {
/* styles that only apply in IE */
}
}
If you also want to add Microsoft Edge into the list, you can do the following. However I do not recommend it as Edge is a much more competent browser than IE.
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident|Edge\//.test(ua);
if ( isIE ) {
//IE & Edge specific code goes here
}
Try this if you are using jquery version >=1.9,
var browser;
jQuery.uaMatch = function (ua) {
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
/(Trident)[\/]([\w.]+)/.exec(ua) || [];
return {
browser: match[1] || "",
version: match[2] || "0"
};
};
// Don't clobber any existing jQuery.browser in case it's different
if (!jQuery.browser) {
matched = jQuery.uaMatch(navigator.userAgent);
browser = {};
if (matched.browser) {
browser[matched.browser] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if (browser.chrome) {
browser.webkit = true;
} else if (browser.webkit) {
browser.safari = true;
}
jQuery.browser = browser;
}
If using jQuery version <1.9 ($.browser was removed in jQuery 1.9) use the following code instead:
$('.myClass').on('click', function (event) {
if ($.browser.msie) {
alert($.browser.version);
}
});
function msieversion() {
var ua = window.navigator.userAgent;
console.log(ua);
var msie = ua.indexOf("MSIE ");
if (msie > -1 || navigator.userAgent.match(/Trident.*rv:11\./)) {
// If Internet Explorer, return version numbe
// You can do what you want only in IE in here.
var version_number=parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)));
if (isNaN(version_number)) {
var rv_index=ua.indexOf("rv:");
version_number=parseInt(ua.substring(rv_index+3,ua.indexOf(".",rv_index)));
}
console.log(version_number);
} else {
//other browser
console.log('otherbrowser');
}
}
You should see the result in console, please use chrome Inspect.
function detectIE() {
var ua = window.navigator.userAgent;
var ie = ua.search(/(MSIE|Trident|Edge)/);
return ie > -1;
}
This is how the Angularjs team is doing it (v 1.6.5):
var msie, // holds major version number for IE, or NaN if UA is not IE.
// Support: IE 9-11 only
/**
* documentMode is an IE-only property
* http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
*/
msie = window.document.documentMode;
Then there are several lines of code scattered throughout using it as a number such as
if (event === 'input' && msie <= 11) return false;
and
if (enabled && msie < 8) {