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 Explorer and abort / cancel it if they are using other browsers so that it only runs for IE users ? The users here would all be on IE8 or higher versions so I would not need to cover IE7 and lower versions.
If I could tell which browser they are using that would be great but is not required.
Example function:
$('.myClass').on('click', function(event)
{
// my function
});
Just adding onto Mario's extremely helpful answer.
If all you want to know is if the browser is IE or not, the code can be simplified down to just:
Update
I recommend this now. It is still very readable and is far less code :)
Thanks to JohnnyFun in the comments for the shortened answer :)
Use below JavaScript method :
You may find the details on below Microsoft support site :
How to determine browser version from script
Update : (IE 11 support)
This returns
true
for any version of Internet Explorer:The
userAgent
parameter is optional and it defaults to the browser's user agent.Below I found elegant way of doing this while googling ---
Try doing like this