how to check browser version and set alert

Viewed 83

I want to create code which will auto get the user's browser version and check it , if it was for example less than 50 popup alert which say "You can not access to this website(Update your Browser)" and open error page.

4 Answers

This is the other solution :

navigator.sayswho= (function(){
var ua= navigator.userAgent;
var tem; 
var M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
    tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
    return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
    tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
    if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');})();

alert(navigator.sayswho);

You can use Browser detect which gives you information regarding your browser and versions

alert(BrowserDetect.browser);
alert(BrowserDetect.version);

Another option is using navigator.appVersion but it was deprecated lately

alert(navigator.appVersion);

Why not just do that serverside prior to even delivering the page ? The HTTP request often includes a user agent header (which is the browser brand/version/etc).

The exact implementation depends on what exactly your "site" is and on what type of webserver it runs.

If it's a PHP application then you could use $_SERVER['HTTP_USER_AGENT'].

If the site uses plain old static .html files and runs on Apache httpd, then you could use RewriteCond %{HTTP_USER_AGENT} ...something...

and so on.

    function get_version() {
  let ua = navigator.userAgent,
    tem,
    M =
      ua.match(
        /(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i
      ) || [];
  if (/trident/i.test(M[1])) {
    tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
    return { name: "IE", version: tem[1] || "" };
  }
  if (M[1] === "Chrome") {
    tem = ua.match(/\bOPR|Edge\/(\d+)/);
    if (tem != null) {
      return { name: "Opera", version: tem[1] };
    }
  }
  M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, "-?"];
  if ((tem = ua.match(/version\/(\d+)/i)) != null) {
    M.splice(1, 1, tem[1]);
  }
  return {
    name: M[0],
    version: M[1],
  };
}

let browser = get_version();

// you can use these two in anywhere 
console.log(browser.name,browser.version)

Related