3

>>>点击获取更多文章<<<

下面核心代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript">
        (function () {
            try {
                var report = function (id, obj,eventtype) {
                    obj.pageurl = location.href;
                    obj.ua = navigator.userAgent; 
                    var json = "{ ";
                    for (var i in obj) {
                        json += "" + i + ":" + obj[i] + ", "
                    }
                    json += "}";
                    var src = "http://www.xxx.com/logservice?eventtype=" + eventtype + "&eventid=" + id +
                    "&extend=";
                    var img = new Image();
                    img.src = src + encodeURIComponent(json);
                    img.onload = img.onerror = function () {
                        img = null
                    }
                };
                if (window.addEventListener) {
                    var ingore = ["localhost"];
                    window.addEventListener("error", function (e) {
                        //抓取文件404报错
                        if (e.target && e.target.src) {
                            var obj = {};
                            obj.file = e.target.src;
                            for (var i = 0, len = ingore.length; i < len; i++) {
                                if (obj.file.indexOf(ingore[i]) > -1) {
                                    return
                                }
                            }
                            report(404, obj,'jserror');
                        }
                    }, true);
                }
                window.onerror = function (msg, file, line) {
                    //抓取js常规报错
                    var obj = {};
                    obj.errmsg = msg;
                    obj.errfile = file;
                    obj.errline = line;
                    report(500, obj,'jserror');
                }

                window.onload = function()
                {
                    setTimeout(function()
                    {
                        if (window.performance && window.performance.timing) {
                            //抓取页面性能时间
                            var perf = window.performance.timing ,obj = {};
                            var arr = ['navigationStart','domainLookupEnd','connectEnd','domContentLoadedEventEnd','loadEventEnd'];
                            arr.forEach(function(curv,i){
                                obj[curv] = perf[curv];
                            });
                            report(200, obj,'performance');
                        }    
                    },1000);
                }


            } catch (e) {}
        })();
    </script>
</head>
<body>
    <img src="http://www.test.com/aa.jpeg">
    <img src="http://www.test.com/bb.jpeg">
    <script type="text/javascript">
        showme1();
    </script>
</body>
</html>

注意 注释 的“抓取文件404报错”,“抓取js常规报错”,“抓取页面性能时间”,代码很好理解,将整个操作放在闭包内执行,以免污染外面。 这段代码必须放在head标签头部内,若头部有js外联,那该段代码必须放在js外联之上,若将这段代码放置在http://www.xxx.com/logservice.js,那代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="http://www.xxx.com/logservice.js"></script>
    <script type="text/javascript" src="其他js"></script>
</head>
<body>
    //常规业务代码
</body>
</html>

在获取页面性能时间方面,先获取,后台再去计算。关于window.performance 方面,文章很多,具体里面每个单词代表的时间意义,可以自行搜查文档。如:前端性能监控方案window.performance 调研(转)


前端技术精髓
1.8k 声望760 粉丝