我需要使用Angular(TypeScript)来检测浏览器是否为IE或Edge。这是否可能?如果可能,该如何实现?
我需要使用Angular(TypeScript)来检测浏览器是否为IE或Edge。这是否可能?如果可能,该如何实现?
我之前使用过这个,效果很好。
const isIEOrEdge = /msie\s|trident\/|edge\//i.test(window.navigator.userAgent)
let
,const
应该更合适。 - Okonomiyaki3000// 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;
!!window['chrome']&& !!window['chrome']['webstore']
可以正常工作。 - Cristian Sepulvedachrome.webstore
未定义。请改用!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
。 - devqon针对仍在查找此线程的人:
如果您使用的是 Angular 10 或更高版本,建议使用 PlatformModule
,该模块已在 Angular Material CDK 的版本 10 中添加。
EDGE: false
是在寻找旧版的Microsoft Edge浏览器,而那个浏览器不是基于Chromium的。 - Scopperloit这对我有用:
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';
}
}
在调用getBrowserName()
之后,您可以将返回值与==='edge'
进行比较,以查看您是否在使用 Edge 浏览器。
case agent.indexOf('edg') > -1: return 'edge';
- James Parkeragent.indexOf('safari') > -1
,则几乎每个现代浏览器都会返回 true,因为大多数浏览器会重复其用户代理字符串的某些部分,因此此检测依赖于进行评估的顺序。 - agbb对于Angular用户,您可以使用此模块npm install ngx-device-detector --save
以上的答案对我都不起作用。
deviceInfo: {"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36","os":"Windows","browser":"Chrome","device":"Unknown","os_version":"windows-10","browser_version":"75.0.3770.100"}
。而且,这是Angular的方式! - BobC浏览器的userAgent会随着时间的推移而不同和发展。与其手动解析userAgent,不如考虑使用一个维护良好的库来确保未来支持。最流行的选项有:
这个优秀的库将从User-Agent数据中分析客户端浏览器、引擎、操作系统、CPU和设备类型/型号,并具有相对较小的占用空间。它得到了高度的维护和极大的普及(目前每周下载量为7.3M)。使用方法很简单:
import { UAParser } from 'ua-parser-js';
const parser = new UAParser();
const result = parser.getResult();
const isEdge = result.browser == 'Edge';
在Angular Material V6.4.7中引入了"Platform"检测模块:
平台:通过比较userAgent字符串和检查特定于浏览器的全局属性来检测当前平台的服务。
关于EDGE的重要说明:自版本79起,EDGE使用Blink浏览器引擎,因此此选项仅适用于旧版EDGE。
导入Platform
服务并直接检查EDGE
(或任何其他浏览器/操作系统):
import { Platform } from '@angular/cdk/platform';
@Injectable()
export class AppService {
isEdge: boolean;
constructor(private platform: Platform) {
this.isEdge = this.platform.EDGE; // or IOS, SAFARI, ANDROID, etc.
}
}
$ npm install ngx-device-detector --save
import { NgModule } from '@angular/core';
import { DeviceDetectorModule } from 'ngx-device-detector';
...
@NgModule({
declarations: [
...
LoginComponent,
SignupComponent
...
],
imports: [
CommonModule,
FormsModule,
DeviceDetectorModule.forRoot()
],
providers:[
AuthService
]
...
})
import { Component } from '@angular/core';
...
import { DeviceDetectorService } from 'ngx-device-detector';
...
@Component({
selector: 'home', // <home></home>
styleUrls: [ './home.component.scss' ],
templateUrl: './home.component.html',
...
})
export class HomeComponent {
deviceInfo = null;
...
constructor(..., private http: Http, private deviceService: DeviceDetectorService) {
this.epicFunction();
}
...
epicFunction() {
console.log('hello `Home` component');
this.deviceInfo = this.deviceService.getDeviceInfo();
const isMobile = this.deviceService.isMobile();
const isTablet = this.deviceService.isTablet();
const isDesktopDevice = this.deviceService.isDesktop();
console.log(this.deviceInfo);
console.log(isMobile); // returns if the device is a mobile device (android / iPhone / windows-phone etc)
console.log(isTablet); // returns if the device us a tablet (iPad etc)
console.log(isDesktopDevice); // returns if the app is running on a Desktop browser.
}
...
}
您可以使用正则表达式和用户代理来检测IE浏览器。
private isIE() {
const match = navigator.userAgent.search(/(?:Edge|MSIE|Trident\/.*; rv:)/);
let isIE = false;
if (match !== -1) {
isIE = true;
}
return isIE;
}
但是,推荐使用特性检测而不是浏览器检测。您可以使用现代化的库来完成这项工作 https://modernizr.com
let match = navigator.userAgent.search(/(?:Edge|MSIE|Trident\/.*; rv:)/);
let isIE = false;
if (match !== -1) {
isIE = true;
}
console.log(isIE,'isIE');
const isChromiumBased =
!!window["chrome"] &&
(!!window["chrome"]["webstore"] || !!window["chrome"]["runtime"]);
!!(window.chrome && (window.chrome.webstore || window.chrome.runtime))
- MrHIDEn