前端相关数据监察和控制

2015/08/16 · HTML5 ·
多少监察和控制

原版的书文出处:
AlloyTeam   

花色开销完结外发后,没有二个督察种类,咱们很难领悟到揭橥出去的代码在客商机器上实行是还是不是科学,所以供给创建前端代码品质相关的督察系列。

所以大家须要做以下的有的模块:

风流洒脱、搜聚脚本实行错误

JavaScript

function error(msg,url,line){ var REPORT_UCR-VL = “xxxx/cgi”; //
搜罗报告数据的信息 var m =[msg, url, line, navigator.userAgent, +new
Date];// 搜集错误音讯,发生错误的脚本文件互联网地址,客户代理新闻,时间
var url = REPORT_U本田CR-VL + m.join(‘||’);// 组装错误报告音信内容UCRUISERL var img
= new Image; img.onload = img.onerror = function(){ img = null; };
img.src = url;// 发送数据到后台cgi } // 监听错误上报 window.onerror =
function(msg,url,line){ error(msg,url,line); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function error(msg,url,line){
   var REPORT_URL = "xxxx/cgi"; // 收集上报数据的信息
   var m =[msg, url, line, navigator.userAgent, +new Date];// 收集错误信息,发生错误的脚本文件网络地址,用户代理信息,时间
   var url = REPORT_URL + m.join(‘||’);// 组装错误上报信息内容URL
   var img = new Image;
   img.onload = img.onerror = function(){
      img = null;
   };
   img.src = url;// 发送数据到后台cgi
}
// 监听错误上报
window.onerror = function(msg,url,line){
   error(msg,url,line);
}

由此拘押后台系统,我们得以看来页面上每回错误的音信,通过这么些新闻我们能够快速定位同一时候消亡难题。

二、收集html5 performance信息

performance
包罗页面加载到实行到位的整个生命周期中不相同实施步骤的推行时间。performance相关文章点击如下:接受performance
API
监测页面质量

总计不相同步骤时间相对于navigationStart的时辰差,能够透过相应后台CGI搜罗。

JavaScript

function _performance(){ var REPORT_URL = “xxxx/cgi?perf=”; var perf =
(window.webkitPerformance ? window.webkitPerformance :
window.msPerformance ), points =
[‘navigationStart’,’unloadEventStart’,’unloadEventEnd’,’redirectStart’,’redirectEnd’,’fetchStart’,’domainLookupStart’,’connectStart’,’requestStart’,’responseStart’,’responseEnd’,’domLoading’,’domInteractive’,’domContentLoadedEventEnd’,’domComplete’,’loadEventStart’,’loadEventEnd’];
var timing = pref.timing; perf = perf ? perf : window.performance; if(
perf && timing ) { var arr = []; var navigationStart =
timing[points[0]]; for(var i=0,l=points.length;i<l;i++){ arr[i]
= timing[points[i]] – navigationStart; } var url = REPORT_URL +
arr.join(“-“); var img = new Image; img.onload = img.onerror =
function(){ img=null; } img.src = url; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function _performance(){
   var REPORT_URL = "xxxx/cgi?perf=";
   var perf = (window.webkitPerformance ? window.webkitPerformance : window.msPerformance ),
      points = [‘navigationStart’,’unloadEventStart’,’unloadEventEnd’,’redirectStart’,’redirectEnd’,’fetchStart’,’domainLookupStart’,’connectStart’,’requestStart’,’responseStart’,’responseEnd’,’domLoading’,’domInteractive’,’domContentLoadedEventEnd’,’domComplete’,’loadEventStart’,’loadEventEnd’];
   var timing = pref.timing;
   perf = perf  ? perf : window.performance;
   if( perf  && timing ) {
      var arr = [];
      var navigationStart = timing[points[0]];
      for(var i=0,l=points.length;i<l;i++){
         arr[i] = timing[points[i]] – navigationStart;
      }
   var url = REPORT_URL + arr.join("-");
   var img = new Image;
   img.onload = img.onerror = function(){
      img=null;
   }
   img.src = url;
}

由从此以后台接口采撷和计算,大家得以对页面推行品质有很详细的刺探。

三、总计各样页面包车型大巴JS和CSS加载时间

在JS或然CSS加载在此以前打上时间戳,加载之后打上时间戳,而且将数据报告到后台。加载时间反映了页面白屏,可操作的等候时间。

XHTML

<script>var cssLoadStart = +new Date</script> <link
rel=”stylesheet” href=”xxx.css” type=”text/css” media=”all”> <link
rel=”stylesheet” href=”xxx1.css” type=”text/css” media=”all”>
<link rel=”stylesheet” href=”xxx2.css” type=”text/css”
media=”all”> <sript> var cssLoadTime = (+new Date) –
cssLoadStart; var jsLoadStart = +new Date; </script> <script
type=”text/javascript” src=”xx1.js”></script> <script
type=”text/javascript” src=”xx2.js”></script> <script
type=”text/javascript” src=”xx3.js”></script> <script>
var jsLoadTime = (+new Date) – jsLoadStart; var REPORT_URL =
‘xxx/cgi?data=’ var img = new Image; img.onload = img.onerror =
function(){ img = null; }; img.src = REPORT_URL + cssLoadTime + ‘-‘ +
jsLoadTime; </script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>var cssLoadStart = +new Date</script>
<link rel="stylesheet" href="xxx.css" type="text/css" media="all">
<link rel="stylesheet" href="xxx1.css" type="text/css" media="all">
<link rel="stylesheet" href="xxx2.css" type="text/css" media="all">
<sript>
   var cssLoadTime = (+new Date) – cssLoadStart;
   var jsLoadStart = +new Date;
</script>
<script type="text/javascript" src="xx1.js"></script>
<script type="text/javascript" src="xx2.js"></script>
<script type="text/javascript" src="xx3.js"></script>
<script>
   var jsLoadTime = (+new Date) – jsLoadStart;
   var REPORT_URL = ‘xxx/cgi?data=’
   var img = new Image;
   img.onload = img.onerror = function(){
      img = null;
   };
   img.src = REPORT_URL + cssLoadTime + ‘-‘ + jsLoadTime;
</script>

XHTML

<a href=””
target=”_blank”> </a>

1
<a href="https://github.com/perltzhu/js-data-report" target="_blank"> </a>

1.让文字不停地滚动

参照他事他说加以侦察资料:

  • html5 performance
    en
  • html5 performance
    cn
  • javascript onerror
    api

    1 赞 1 收藏
    评论

图片 1

  <MA普拉多QUEE>滚动文字</MARubiconQUEE>

2.记下并出示网页的最终校订时间

  <script language=Javascript>
  document.write(“最终更新时间: ” + document.lastModified + “”)
  </script>

3.关门当前窗口

  <a href=”/”onClick=”javascript:window.close();return
false;”>关闭窗口</a>

4.5秒后关闭当前页

  <script language=”Javascript”>
  <!–
  setTimeout(‘window.close();’,5000);
  –>
  </script>

5.2秒后载入钦赐网页

  <head>
  <meta http-equiv=”refresh” content=”2;URL=
  </head>

6.加多到收藏夹

  <script Language=”Javascript”>
  function bookmarkit()
  {
  window.external.addFavorite(”)
  }
  if (document.all)document.write(‘<a href=”#”
onClick=”bookmarkit()”>出席收藏夹</a>’)
  </script>

7.让超链接不展现下划线

  <style type=”text/css”>
  <!-
  a:link{text-decoration:none}
  a:hover{text-decoration:none}
  a:visited{text-decoration:none}
  ->
  </style>

8.禁绝鼠标右键的动作

  <script Language = “Javascript”>
  function click() { if (event.button==2||event.button==3)
  {
  alert(‘禁绝鼠标右键’);
  }
  document.onmousedown=click // –>
  </script>

9.设置该页为首页

  <body bgcolor=”#FFFFFF” text=”#000000″>
  <!– 网址:
  <a class=”chlnk” style=”cursor:hand” HREF
  onClick=”this.style.behavior=’url(#default#homepage)’;
  this.setHomePage(‘你的网址名称);”><font color=”000000″ size=”2″
face=”燕体”>设为首页</font></a>
  </body>

10.纪念日倒计时

  <script Language=”Javascript”>
  var timedate= new Date(“December 25,2003”);
  var times=”圣诞节”;
  var now = new Date();
  var date = timedate.getTime() – now.getTime();
  var time = Math.floor(date / (1000 * 60 * 60 * 24));
  if (time >= 0)
  document.write(“现在离”+times+”还有: “+time +”天”)</script>

11.单击开关打字与印刷出当前页

  <script Language=”Javascript”>
  <!– Begin
  if (window.print) {
  document.write(‘<form>’
  + ‘<input type=button name=print value=”打字与印刷本页” ‘
  + ‘onClick=”javascript:window.print()”></form>’);
  }
  // End –>
  </script>

12.单击按键‘另存为’当前页

  <input type=”button” name=”Button” value=”保存本页”
  onClick=”document.all.button.ExecWB(4,1)”>
  <object id=”button”
  width=0
  height=0
  classid=”CLSID:8856F961-340A-11D0-A96B-00C04FD705A2″>
  <embed width=”0″ height=”0″></embed>
  </object>

相关文章