【问题标题】:Angular 6 browser detection?Angular 6浏览器检测?
【发布时间】:2019-02-10 13:27:15
【问题描述】:

我有一个仅支持 chrome 的 angular6 应用程序,但我希望显示用户尝试导航到 IE 中的应用程序的页面/消息,说它不受支持,或者将此链接粘贴到 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;

想法?谢谢!

【问题讨论】:

标签: angular typescript browser angular6 browser-detection


【解决方案1】:

在 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
}

【讨论】:

    【解决方案2】:

    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') &gt; -1 对于 Microsoft Edge Beta(当前版本=79.0.309.43)失败,因为 userAgent 是

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

    【讨论】:

      【解决方案3】:

      您也可以在 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>
      

      【讨论】:

        猜你喜欢
        • 2016-01-01
        • 2018-06-19
        • 2016-12-11
        • 2011-01-14
        • 1970-01-01
        • 2011-04-19
        • 2011-09-24
        • 2011-07-25
        相关资源
        最近更新 更多