How do I detect what browser is used to access my site?

前端 未结 11 1996
盖世英雄少女心
盖世英雄少女心 2021-01-13 13:51

How do I detect what browser (IE, Firefox, Opera) the user is accessing my site with? Examples in Javascript, PHP, ASP, Python, JSP, and any others you can think of would b

相关标签:
11条回答
  • 2021-01-13 13:58

    A quick and dirty java servlet example

    private String getBrowserName(HttpServletRequest request) {
        // get the user Agent from request header
        String userAgent = request.getHeader(Constants.BROWSER_USER_AGENT);
        String BrowesrName = "";
        //check for Internet Explorer
        if (userAgent.indexOf("MSIE") > -1) {
            BrowesrName = Constants.BROWSER_NAME_IE;
        } else if (userAgent.indexOf(Constants.BROWSER_NAME_FIREFOX) > -1) {
            BrowesrName = Constants.BROWSER_NAME_MOZILLA_FIREFOX;
        } else if (userAgent.indexOf(Constants.BROWSER_NAME_OPERA) > -1) {
            BrowesrName = Constants.BROWSER_NAME_OPERA;
        } else if (userAgent.indexOf(Constants.BROWSER_NAME_SAFARI) > -1) {
            BrowesrName = Constants.BROWSER_NAME_SAFARI;
        } else if (userAgent.indexOf(Constants.BROWSER_NAME_NETSCAPE) > -1) {
            BrowesrName = Constants.BROWSER_NAME_NETSCAPE;
        } else {
            BrowesrName = "Undefined Browser";
        }
        //return the browser name
        return BrowesrName;
    }
    
    0 讨论(0)
  • 2021-01-13 14:00

    You would take a look at the User-Agent that they are sending. Note that you can send whatever agent you want, so that's not 100% foolproof, but most people don't change it unless there's a specific reason to.

    0 讨论(0)
  • 2021-01-13 14:02

    If it's for handling the request, look at the User-Agent header on the incoming request.

    UPDATE: If it's for reporting, configure your web server to log the User-Agent in the access logs, then run a log analysis tool, e.g., AWStats.

    UPDATE 2: FYI, it's usually (not always, usually) a bad idea to change the way you're handling a request based on the User-Agent.

    0 讨论(0)
  • 2021-01-13 14:04

    On the client side, you can do this in Javascript using the navigation.userAgent object. Here's a crude example:

    if (navigator.userAgent.indexOf("MSIE") > -1) 
    {
        alert("Internet Explorer!");
    }
    else if (navigator.userAgent.indexOf("Firefox") > -1)
    {
        alert("Firefox!");
    }
    

    A more detailed and comprehensive example can be found here: http://www.quirksmode.org/js/detect.html

    Note that if you're doing the browser detection for the sake of Javascript compatibility, it's usually better to simply use object detection or a try/catch block, lest some version you didn't think of slip through the cracks of your script. For example, instead of doing this...

    if(navigator.userAgent.indexOf("MSIE 6") > -1)
    {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        objXMLHttp = new XMLHttpRequest();
    }
    

    ...this is better:

    if(window.XMLHttpRequest) // Works in Firefox, Opera, and Safari, maybe latest IE?
    {
        objXMLHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) // If the above fails, try the MSIE 6 method
    {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    0 讨论(0)
  • 2021-01-13 14:10

    First of all, I'd like to note, that it is best to avoid patching against specific web-browsers, unless as a last result -try to achieve cross-browser compatibility instead using standard-compliant HTML/CSS/JS (yes, javascript does have a common denominator subset, which works across all major browsers).

    With that said, the user-agent tag from the HTTP request header contains the client's (claimed) browser. Although this has become a real mess due to people working against specific browser, and not the specification, so determining the real browser can be a little tricky.

    Match against this:

    contains browser

    Firefox -> Firefox

    MSIE -> Internet Explorer

    Opera -> Opera (one of the few browsers, which don't pretend to be Mozilla :) )

    Most of the agents containing the words "bot", or "crawler" are usually bots (so you can omit it from logs / etc)

    0 讨论(0)
  • 2021-01-13 14:11

    You can use the HttpBrowserCapabilities Class in ASP.NET. Here is a sample from this link

    private void Button1_Click(object sender, System.EventArgs e)
    {
            HttpBrowserCapabilities bc;
            string s;
            bc = Request.Browser;
            s= "Browser Capabilities" + "\n";
            s += "Type = " + bc.Type + "\n";
            s += "Name = " + bc.Browser + "\n";
            s += "Version = " + bc.Version + "\n";
            s += "Major Version = " + bc.MajorVersion + "\n";
            s += "Minor Version = " + bc.MinorVersion + "\n";
            s += "Platform = " + bc.Platform + "\n";
            s += "Is Beta = " + bc.Beta + "\n";
            s += "Is Crawler = " + bc.Crawler + "\n";
            s += "Is AOL = " + bc.AOL + "\n";
            s += "Is Win16 = " + bc.Win16 + "\n";
            s += "Is Win32 = " + bc.Win32 + "\n";
            s += "Supports Frames = " + bc.Frames + "\n";
            s += "Supports Tables = " + bc.Tables + "\n";
            s += "Supports Cookies = " + bc.Cookies + "\n";
            s += "Supports VB Script = " + bc.VBScript + "\n";
            s += "Supports JavaScript = " + bc.JavaScript + "\n";
            s += "Supports Java Applets = " + bc.JavaApplets + "\n";
            s += "Supports ActiveX Controls = " + bc.ActiveXControls + "\n";
            TextBox1.Text = s;
    }
    
    0 讨论(0)
提交回复
热议问题