如何用Angular检测浏览器?

Liu*_*tas 35 browser-detection typescript angular

我需要检测浏览器是IE还是Edge with Angular(TypeScript).可能吗?如果是这样,怎么样?

arg*_*goo 55

我以前用过这个,效果很好.

const isIEOrEdge = /msie\s|trident\/|edge\//i.test(window.navigator.userAgent)
Run Code Online (Sandbox Code Playgroud)

  • 我认为您不需要在这里使用`let`,`const`应该更合适。 (2认同)

I. *_*med 23

请使用以下代码:

// 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;
    // If isChrome is undefined, then use:
    var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
    // 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;
Run Code Online (Sandbox Code Playgroud)

  • angular cli使用window,chrome抛出错误,但它使用`!! window ['chrome'] && !! window ['chrome'] ['webstore']` (3认同)
  • @Liutas Angular正在使用Typescript。它是JavaScript的超集。当然可以。 (2认同)
  • 此代码现在对所有浏览器都显示为false。在Win7上从Chrome启动 (2认同)

Sco*_*oit 13

对于仍然发现此线程的人:

如果您使用的是 Angular 10 或更高版本,我建议使用PlatformModule,它已添加到版本 10 中的 Angular Material CDK。

https://material.angular.io/cdk/platform/api

  • @JamesParker 那是因为你使用的是新的 Edge 浏览器,它实际上是基于 Chromium 的。如果您改为检查 Blink,它可能会返回 true。Blink 是浏览器布局引擎,也是 Chromium 的一部分,因此对于 Edge 和 Chrome 来说都是如此。我相信“EDGE: false”正在寻找旧的 Microsoft Edge 浏览器,它*不*基于 Chromium。 (4认同)

小智 10

这对我有用:

  public getBrowserName() {
    const agent = window.navigator.userAgent.toLowerCase()
    switch (true) {
      case agent.indexOf('edge') > -1:
        return 'edge';
      case agent.indexOf('opr') > -1 && !!(<any>window).opr:
        return 'opera';
      case agent.indexOf('chrome') > -1 && !!(<any>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';
    }
}
Run Code Online (Sandbox Code Playgroud)

调用后getBrowserName()可以比较返回值,==='edge'看是否是在边缘浏览器中操作。


pen*_*Dev 7

对于 angular 用户,您可以使用此模块 npm install ngx-device-detector --save

以上答案对我不起作用