github_35883572
ListenerZ
采纳率50%
2016-09-08 15:19

JS客户端检测,为什么总是Uncaught ReferenceError 错误?

已采纳
 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
        var client = function () {
            var engine = {
                //内核
                ie: 0,
                gecko: 0,
                webkit: 0,
                khtml:0,
                opera:0,
                //具体版本
                 ver: null
            };
            var browser = {
                //浏览器
                ie: 0,
                firefox: 0,
                safari: 0,
                konq: 0,
                opera: 0,
                chrome: 0,
                //具体的版本
                ver: null
            };
            return {
                engine: engine,
                browser: browser
            };
        }();
        var ua = navigator.userAgent;
        if (window.opener) {
            engine.ver =browser.ver = window.opera.version();
            engine.opera = browser.opera = parseFloat(engine.ver);
        }else if (/AppleWebKit\/(\S+)/.test(ua)) {
            engine.ver = RegExp["$1"];
            engine.webkit = parseFloat(engine.ver);
            //确定是Chrome还是Safari
            if (/Chrome\/(\S+)/.test(ua)) {
                browser.ver = RegExp["$1"];
                browser.chrome = parseFloat(browser.ver);
            }else{
                //近似地确定版本号
                var safariVersion = 1;
                if (engine.webkit < 100) {
                    safariVersion = 1;
                }else if (engine.webkit < 312) {
                    safariVersion = 1.2;
                }else if (engine.webkit < 412) {
                    safariVersion = 1.3;
                }else {
                    safariVersion = 2;
                }
                browser.safari = browser.ver = safariVersion;
            }
        }else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
            engine.ver = RegExp["$1"];
            engine.khtml = browser.konq = parseFloat(engine.ver);
        }else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)) {
            engine.ver = RegExp["$1"];
            engine.khtml = browser.konq = parseFloat(browser.ver);
            //确定是不是Firefox
            if (/firefox\/(\S+)/.test(ua)) {
                browser.ver = RegExp["$1"];
                browser.firefox = parseFloat(browser.ver);
            }
        }else if (/MSIE ([^;]+)/.test(ua)) {
            engine.ver = browser.ver = RegExp["$1"];
            engine.ie = browser.ie = parseFloat(engine.ver);
        }
        if (client.engine.webkit){
            if(client.browser.chrome){
                alert("使用的浏览器是chrome")
            }else if(client.browser.safari){
                alert("浏览器是safari");
            }else if (client.engine.gecko){
                if (client.browser.firefox){
                    alert("浏览器是fir");
                }else {

                }
            }
        }
    </script>
</head>
<body>

</body>
</html>

总是出现Uncaught ReferenceError: engine is not defined;
为什么变量保存了调用函数后的值 engine 仍然是未定义的?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_18128329 falost 5年前

    首先,你声明了变量,是一个局部的变量,你让这个方法自调用了,并且 return 了出来,但是 你用一个变量 接住了这个return的值 如果想调用的话 需要 在调用的时候 增加 一个 client 来读取他的属性

    修改后的代码 如下:

    var client = function () {
    var engine = {
    //内核
    ie: 0,
    gecko: 0,
    webkit: 0,
    khtml:0,
    opera:0,
    //具体版本
    ver: null
    };
    var browser = {
    //浏览器
    ie: 0,
    firefox: 0,
    safari: 0,
    konq: 0,
    opera: 0,
    chrome: 0,
    //具体的版本
    ver: null
    };
    return {
    engine: engine,
    browser: browser
    };
    }();
    var ua = navigator.userAgent;
    if (window.opener) {
    client.engine.ver =client.browser.ver = window.opera.version();
    client.engine.opera = client.browser.opera = parseFloat(client.engine.ver);
    }else if (/AppleWebKit\/(\S+)/.test(ua)) {
    client.engine.ver = RegExp["$1"];
    client.engine.webkit = parseFloat(client.engine.ver);
    //确定是Chrome还是Safari
    if (/Chrome\/(\S+)/.test(ua)) {
    client.browser.ver = RegExp["$1"];
    client.browser.chrome = parseFloat(client.browser.ver);
    }else{
    //近似地确定版本号
    var safariVersion = 1;
    if (engine.webkit < 100) {
    safariVersion = 1;
    }else if (engine.webkit < 312) {
    safariVersion = 1.2;
    }else if (engine.webkit < 412) {
    safariVersion = 1.3;
    }else {
    safariVersion = 2;
    }
    client.browser.safari = client.browser.ver = safariVersion;
    }
    }else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
    client.engine.ver = RegExp["$1"];
    client.engine.khtml = client.browser.konq = parseFloat(client.engine.ver);
    }else if (/rv:([^)]+)) Gecko\/\d{8}/.test(ua)) {
    client.engine.ver = RegExp["$1"];
    client.engine.khtml = client.browser.konq = parseFloat(client.browser.ver);
    //确定是不是Firefox
    if (/firefox\/(\S+)/.test(ua)) {
    client.browser.ver = RegExp["$1"];
    client.browser.firefox = parseFloat(client.browser.ver);
    }
    }else if (/MSIE ([^;]+)/.test(ua)) {
    client.engine.ver = client.browser.ver = RegExp["$1"];
    client.engine.ie = client.browser.ie = parseFloat(client.engine.ver);
    }
    if (client.engine.webkit){
    if(client.browser.chrome){
    alert("使用的浏览器是chrome")
    }else if(client.browser.safari){
    alert("浏览器是safari");
    }else if (client.engine.gecko){
    if (client.browser.firefox){
    alert("浏览器是fir");
    }else {

        }
      }
    }
    
    
    
    点赞 2 评论 复制链接分享

相关推荐