0

I need to detect browser type (IE, Chrome, Safari etc.) in the lightning component but navigator.userAgent is not working in lightning.

3
  • Ideally if designed, lightning component should behave same in all devices, you you want any specific funcionality to be only availabe for phone etc? Commented Nov 28, 2018 at 11:37
  • It seems like a duplicate to this post Commented Nov 28, 2018 at 11:47
  • @Taruna, mind sharing your code and what doesn’t seem to be working?
    – glls
    Commented Nov 28, 2018 at 12:04

2 Answers 2

4

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...

Properties of $Browser

0

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");
}

} })

1
  • this javascript snippet does not appear to be working anymore in the iPad Chrome
    – jojopogi
    Commented Jan 14, 2021 at 23:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .