ListenerZ
2016-09-08 15:19
采纳率: 50%
浏览 2.4k
已采纳

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条回答 默认 最新

相关推荐 更多相似问题