What browser is the enduser using

I am trying to find out what browser the enduser of my Anvil app is using.

I have tried: user_agent = anvil.js.window.navigator.userAgent

but this gives me the following output which is not usable:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

I need to know if the browser is: Chrome, Edge or Safari.
How should I do that?

You can use the user_agent like this:

def detect_browser():
    user_agent = anvil.js.window.navigator.userAgent
    if "Edg" in user_agent:
        return "Edge"
    elif "Chrome" in user_agent:
        return "Chrome"
    elif "Safari" in user_agent:
        return "Safari"
    return "Other"

This is not as simple as it first seems. Good write up at

2 Likes