Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Detect browser IE9 and below and throw up a modal to upgrade

I want to be able to detect IE9 or less using jQuery (or if there's a better method?). If the browser version is IE9 or less I then want to load a modal with the option to upgrade to Chrome, FF etc.

I have read that $.browser doesn't work anymore, so just wondering what the best way is to achieve what I want:

$(document).ready(function(){

   /* Get browser */
   $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

   /* Detect Chrome */
   if($.browser.chrome){
      /* Do something for Chrome at this point */
      alert("You are using Chrome!");

      /* Finally, if it is Chrome then jQuery thinks it's 
       Safari so we have to tell it isn't */
       $.browser.safari = false;
  }

  /* Detect Safari */
  if($.browser.safari){
      /* Do something for Safari */
      alert("You are using Safari!");
  }

});
like image 945
John Avatar asked Oct 24 '13 09:10

John


2 Answers

var browser = {
        isIe: function () {
            return navigator.appVersion.indexOf("MSIE") != -1;
        },
        navigator: navigator.appVersion,
        getVersion: function() {
            var version = 999; // we assume a sane browser
            if (navigator.appVersion.indexOf("MSIE") != -1)
                // bah, IE again, lets downgrade version number
                version = parseFloat(navigator.appVersion.split("MSIE")[1]);
            return version;
        }
    };

if (browser.isIe() && browser.getVersion() <= 9) {

    console.log("You are currently using Internet Explorer" + browser.getVersion() + " or are viewing the site in Compatibility View, please upgrade for a better user experience.")
}
like image 43
Sam Jones Avatar answered Oct 14 '22 09:10

Sam Jones


var isIE9OrBelow = function()
{
   return /MSIE\s/.test(navigator.userAgent) && parseFloat(navigator.appVersion.split("MSIE")[1]) < 10;
}
like image 84
xicooc Avatar answered Oct 14 '22 07:10

xicooc