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
Many answers here, and I'd like to add my input. IE 11 was being such an ass concerning flexbox (see all its issues and inconsistencies here) that I really needed an easy way to check if a user is using any IE browser (up to and including 11) but excluding Edge, because Edge is actually pretty nice.
Based on the answers given here, I wrote a simple function returning a global boolean variable which you can then use down the line. It's very easy to check for IE.
var isIE;
(function() {
var ua = window.navigator.userAgent,
msie = ua.indexOf('MSIE '),
trident = ua.indexOf('Trident/');
isIE = (msie > -1 || trident > -1) ? true : false;
})();
if (isIE) {
alert("I am an Internet Explorer!");
}
This way you only have to do the look up once, and you store the result in a variable, rather than having to fetch the result on each function call. (As far as I know you don't even have to wait for document ready to execute this code as the user-agent is not related to the DOM.)
Using modernizr
Modernizr.addTest('ie', function () {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ') > 0;
var ie11 = ua.indexOf('Trident/') > 0;
var ie12 = ua.indexOf('Edge/') > 0;
return msie || ie11 || ie12;
});
@SpiderCode's solution does not work with IE 11. Here is the best solution that I used henceforth in my code where I need browser detection for particular feature.
IE11 no longer reports as MSIE, according to this list of changes, it's intentional to avoid mis-detection.
What you can do if you really want to know it's IE is to detect the Trident/ string in the user agent if navigator.appName returns Netscape, something like (the untested);
Thanks to this answer
function isIE()
{
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
else if (navigator.appName == 'Netscape')
{
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv == -1 ? false: true;
}
This returns true
for any version of Internet Explorer:
function isIE(userAgent) {
userAgent = userAgent || navigator.userAgent;
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1;
}
The userAgent
parameter is optional and it defaults to the browser's user agent.
You can dectect all Internet Explorer (Last Version Tested 12).
<script>
var $userAgent = '';
if(/MSIE/i['test'](navigator['userAgent'])==true||/rv/i['test'](navigator['userAgent'])==true||/Edge/i['test'](navigator['userAgent'])==true){
$userAgent='ie';
} else {
$userAgent='other';
}
alert($userAgent);
</script>
See here https://jsfiddle.net/v7npeLwe/
JavaScript function to detect the version of Internet Explorer or Edge
function ieVersion(uaString) {
uaString = uaString || navigator.userAgent;
var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
if (match) return parseInt(match[2])
}