I need to detect browser type (IE, Chrome, Safari etc.) in the lightning component but navigator.userAgent is not working in lightning.
-
Ideally if designed, lightning component should behave same in all devices, you you want any specific funcionality to be only availabe for phone etc?– Pranay JaiswalCommented Nov 28, 2018 at 11:37
-
It seems like a duplicate to this post– Bartosz ŚliwińskiCommented Nov 28, 2018 at 11:47
-
@Taruna, mind sharing your code and what doesn’t seem to be working?– gllsCommented Nov 28, 2018 at 12:04
Add a comment
|
2 Answers
There's a much easier way of doing this natively using the $Browser value provider. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/expr_browser_value_provider.htm
You can access these values using e.g. $A.get('$Browser.isDesktop')
in the Javascript.
Here's is an example of the properties available...
you can use this function to detect a browser in Lightning.
({
checkBrowser: function (component) {
var browserType = 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) || [];
alert("IE"+'IE '+(tem[1] || ''));
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');
alert('chrome');
}
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(' ');
})();
if (browserType.startsWith("IE")) {
component.set("v.isIE", true);
alert("IE");
}
} })
-
this javascript snippet does not appear to be working anymore in the iPad Chrome– jojopogiCommented Jan 14, 2021 at 23:59