Show a message if the browser is not internet explorer 9 or greater

后端 未结 10 1342
北恋
北恋 2020-12-31 01:18

I would like to show my users a bar that looks like this, if:

  1. Browser is not IE; or
  2. Browser is IE but is version 8 or earlier

10条回答
  •  清酒与你
    2020-12-31 01:48

    Checking if browser engine is Trident 6+ (IE 9, 10, 11) should do (demo):

    (function () {
      var trident = {
        string: navigator.userAgent.match(/Trident\/(\d+)/)
      };
    
      trident.version = trident.string ? parseInt(trident.string[1], 10) : null;
    
      if (!trident.string || trident.version < 6) {
        document.body.innerHTML = '
    Not supported.
    ' + document.body.innerHTML; } })();

    However, the sniffing may break in IE 11 final or future versions if Microsoft will decide to change userAgent string.

提交回复
热议问题