0

I was surprised to not be able to find a satisfying solution to this problem (in particular I'm looking for a solution that works for mobile phones as well). I tried something like this:

    if ($.browser.mozilla) {
        // do something
    } else {
        // do something else
    }

but obtained the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'mozilla')
    at (index):120:19

Thank you in advance for your help!

f10w
  • 1,524
  • 4
  • 24
  • 39
  • `$.browser` is a property on the jQuery object. Do you have jQuery imported? – Sebastian Simon Dec 18 '22 at 22:22
  • 2
    This feels like an XY problem to me: why do you need to target Firefox specifically? UA detection should be avoided as much as possible, and you should rely on _feature_ detection instead. – Terry Dec 18 '22 at 22:25
  • You can use feature detection to determine the browser used as explained in this question https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browsers – rowan-vr Dec 18 '22 at 22:25
  • @Terry Paypal popup doesn't work reliably on Firefox. This is a known issue. So for Firefox I should open the payment link in a new tab and not in a popup window. I've found a solution which seems to work, I've posted it as an answer. Thanks for your interest. – f10w Dec 18 '22 at 22:29
  • @rowan-vr Thanks. Were you referring to this solution: `typeof InstallTrigger !== 'undefined';`? – f10w Dec 18 '22 at 22:30
  • 1
    Popups don't work reliably in _any_ browser, because all the major browsers have popup blocking built in, and and most of them block all popups by default. Popups are _never_ the solution. – Zac Anger Dec 18 '22 at 22:31
  • @f10w Yep, hope it helps! – rowan-vr Dec 18 '22 at 22:31
  • @ZacAnger I'm not an expert, but why would PayPal propose the popup solution then? Are they really that incompetent? Please see https://developer.paypal.com/sdk/donate/. – f10w Dec 18 '22 at 22:57
  • @rowan-vr Woah, that's a very interesting solution! But I'm not sure I see why it works though. Could you please elaborate a little bit? Why on earth `typeof InstallTrigger !== 'undefined';` can serve as a check for Firefox browser??? – f10w Dec 18 '22 at 22:59
  • Detecting Firefox UA is simply a stop-gap solution, as there is no guarantee that other browsers will not block Paypal popups in the future. Also, what if a user manually disabled all pop-ups for other non-Firefox browsers? – Terry Dec 18 '22 at 23:02
  • @f10w Wasn't too sure about it before either, however after some research I found out that it is some Non-Standard feature from the Mozilla Apps API for installing apps/extentions on firefox, hence this feature is only defined in Firefox and therefore is the type is not undefined whereas for other browers it is not implemented and thus is undefined – rowan-vr Dec 18 '22 at 23:07
  • @Terry All the browsers on my Mac (Chrome, Safari, Firefox) have popups disabled in preferences for all websites, yet the PayPal Donation popup still shows up. Maybe the PayPal developer used a different popup mechanism? – f10w Dec 18 '22 at 23:10
  • @rowan-vr Thanks, that totally makes sense. Thanks also to the genius who came up with this solution. – f10w Dec 18 '22 at 23:11
  • @f10w Because PayPal's donation flow hasn't changed significantly in nearly a decade. Their checkout flow has more options, but my guess is that Donations don't make enough money for them to want to spend time on more up-to-date solutions. I don't think it's incompetence, probably just lack of attention to a feature that's not especially profitable. And sorry, if I'd known you were working with the Donate button I wouldn't have phrased that so strongly. – Zac Anger Dec 18 '22 at 23:27

2 Answers2

2

I found a solution that seems to work:

let userAgent = navigator.userAgent;
if(userAgent.match(/firefox|fxios/i))
{
   // do something
}
f10w
  • 1,524
  • 4
  • 24
  • 39
0

You can get the user agent via navigator.userAgent. You could then use a regex or simply a .indexOf("BROWSERNAME") to check for different types of browsers.

Teddy
  • 452
  • 1
  • 12