Angular 2:检查用户的浏览器是否兼容 [英] Angular 2: Check whether user's browser is compatible

查看:326
本文介绍了Angular 2:检查用户的浏览器是否兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Angular 2应用程序,但我意识到有些用户可能没有使用可以支持Angular 2的浏览器。

I'm writing an Angular 2 app but am conscious that some users may not be using a browser that can support Angular 2.

我检查是否Javascript已启用,我更感兴趣的是用户的浏览器是否不支持Angular 2所需的某些JS / HTML5 /其他功能。

I have a check for whether Javascript is enabled, I am more interested in whether the user's browser doesn't support certain JS/HTML5/other features required by Angular 2.

最好的方法是什么评估用户的浏览器是否支持Angular 2,如果没有则显示消息?

What would be the best way to assess whether the user's browser supports Angular 2, and display a message if not?

我知道例如 Modernizer ,但不知道从哪里开始,因为Modernizer的重点似乎是逐步组装兼容性检查而不是提供检查整个框架兼容性的解决方案。

I'm aware of e.g. Modernizer, but not sure where to begin as Modernizer's focus seems to be on assembling compatibility checks piecemeal rather than providing a solution that checks for compatibility of whole frameworks.

推荐答案


Angular 2,并显示一条消息如果没有?

Angular 2, and display a message if not?

角度支持的浏览器版本可能不是您应用的浏览器版本(可能更多或它可能更少)。

The version of browsers that angular supports officially might not be the version of browsers your app works (it may be more or it may be less).

您必须自己进行单独的浏览器检查。例如检测IE: http://codepen.io/gapcode/pen/vEJNZN

You have to do individual browser checks yourself. e.g. Detect IE : http://codepen.io/gapcode/pen/vEJNZN

/**
 * detect IE
 * returns version of IE or false, if browser is not Internet Explorer
 */
function detectIE() {
  var ua = window.navigator.userAgent;

  // Test values; Uncomment to check result …

  // IE 10
  // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';

  // IE 11
  // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';

  // IE 12 / Spartan
  // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';

  // Edge (IE 12+)
  // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';

  var msie = ua.indexOf('MSIE ');
  if (msie > 0) {
    // IE 10 or older => return version number
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  }

  var trident = ua.indexOf('Trident/');
  if (trident > 0) {
    // IE 11 => return version number
    var rv = ua.indexOf('rv:');
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  }

  var edge = ua.indexOf('Edge/');
  if (edge > 0) {
    // Edge (IE 12+) => return version number
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  }

  // other browser
  return false;
}

这篇关于Angular 2:检查用户的浏览器是否兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆