Angular 6浏览器检测?

时间:2018-09-05 11:51:09

标签: angular typescript browser angular6 browser-detection

我有一个仅支持chrome的angular6应用程序,但是我希望用户尝试在IE中导航到该应用程序时显示页面/消息,说它不受支持,或者将此链接粘贴到chrome中或下载Chrome。

我需要使用polyfill来使应用程序在IE上运行以显示此消息,还是可以使用某种浏览器检测功能来仅显示此弹出窗口?

我知道您可以在TS中执行浏览器检测,但这意味着我需要IE polyfill,这样应用程序才能加载甚至显示页面。

// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';

// Safari 3.0+ "[object HTMLElementConstructor]" 
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);

// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;

// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;

// Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;

// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;

var output = 'Detecting browsers by ducktyping:<hr>';
output += 'isFirefox: ' + isFirefox + '<br>';
output += 'isChrome: ' + isChrome + '<br>';
output += 'isSafari: ' + isSafari + '<br>';
output += 'isOpera: ' + isOpera + '<br>';
output += 'isIE: ' + isIE + '<br>';
output += 'isEdge: ' + isEdge + '<br>';
output += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = output;

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:0)

在app.component.ts的ngOninit中,使用if else块。在下面的示例中,用户在关闭警报后将被带回到上一页面(非Angular),因此其余的angular不会加载。

if (isIE) {
    alert('Message for users about not using IE');
    window.history.go(-1);
} else {
    // the rest of ngOnInit
}

答案 1 :(得分:0)

您也可以在 index.html 中添加它:

  <script>
    var browserName = getBrowserName();
    if (browserName == 'ie') {
       alert("IE is not supported")
    }

    function getBrowserName() {
      const agent = window.navigator.userAgent.toLowerCase()
      switch (true) {
        case agent.indexOf('edge') > -1:
          return 'edge';
        case agent.indexOf('opr') > -1 && !!(window).opr:
          return 'opera';
        case agent.indexOf('chrome') > -1 && !!(window).chrome:
          return 'chrome';
        case agent.indexOf('trident') > -1:
          return 'ie';
        case agent.indexOf('firefox') > -1:
          return 'firefox';
        case agent.indexOf('safari') > -1:
          return 'safari';
        default:
          return 'other';
      }
    }
  </script>

答案 2 :(得分:0)

window.navigator.userAgent将用户代理保持为string。要检测浏览器,需要解析userAgent

要检测Microsoft Internet Explorer和Microsoft Edge,请使用以下条件

window.navigator.userAgent.toLowerCase().indexOf('trident') > -1 || 
window.navigator.userAgent.toLowerCase().indexOf('edge') > -1


但是最好不要重新发明轮子。相反,请使用bowser npm install bowser

之类的软件包

现在导入并获取详细信息

import * as Bowser from "bowser";
// ......
userAgent = Bowser.parse(window.navigator.userAgent);
browser = Bowser.getParser(window.navigator.userAgent);

示例:https://stackblitz.com/edit/angular-bowser

P.S:另外请注意,navigator.userAgent.toLowerCase().indexOf('edge') > -1对于Microsoft Edge Beta(当前版本为79.0.309.43)失败,因为userAgent是

  

Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 79.0.3945.74 Safari / 537.36 Edg / 79.0.309.43

相关问题