使用Angular进行浏览器检测 [英] Browser detection using Angular
问题描述
我的项目中要求用户使用旧浏览器时显示过时的浏览器消息.
I have a requirement in my project to show outdated browser message when user uses old browser.
我正在使用角度1.5.5.我尝试使用在支持角度的浏览器上工作的angular-bowser模块,但问题出在IE8之类的旧版本上,该版本不支持我的angular版本.因此,角度弓箭模块不起作用.
I am using angular 1.5.5. I tried with angular-bowser module which works on angular supported browser, but the problem comes with old versions like IE8, which doesn't support my angular version . So angular-bowser module doesn't work .
有人可以让我知道其他任何方式或图书馆或任何有帮助的东西吗?
Can somebody let me know about any other ways or some library or anything in that matter that can help?
推荐答案
因为angularjs不仅依赖于angular模块,还可以使用本机javascript来检测浏览器版本:
because angularjs doesn't just depend on angular modules you can use native javascript like so to detect the browser version:
JAVASCRIPT:
navigator.sayswho= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');
})();
//Invoke
navigator.sayswho;
您可以使用此功能确定您的角度应用程序中的当前浏览器和版本,并进行相应的消息对话框.像
You can use this function to determine current browser and version in your angular app and do your message dialog accordingly. Something like
JAVASCRIPT
var version = navigator.sayswho;
if (version <= 8) {
alert("Browser outdated! Please update browser!");
return false; //don't forget.
}
这篇关于使用Angular进行浏览器检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!