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

前端 未结 11 2012
盖世英雄少女心
盖世英雄少女心 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;
    }
    

提交回复
热议问题