javascript常用小例子,前端相关数据监察和控制

javascript常用小例子,前端相关数据监察和控制

前者相关数据监控

2015/08/16 · HTML5 ·
数码监控

初稿出处:
AlloyTeam   

花色开发到位外发后,未有2个监察体系,大家很难精晓到发表出去的代码在用户机器上执行是不是正确,所以要求树立前端代码质量相关的监督体系。

于是大家必要做以下的1些模块:

一、收集脚本执行错误

JavaScript

function error(msg,url,line){ var REPORT_U逍客L = “xxxx/cgi”; //
收集报告数据的新闻 var m =[msg, url, line, navigator.userAgent, +new
Date];// 收集错误新闻,发生错误的本子文件网络地址,用户代理新闻,时间
var url = REPOLacrosseT_U奥迪Q7L + m.join(‘||’);// 组装错误报告新闻内容U奥迪Q三L 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;
}

透过后台接口收集和总计,大家得以对页面执行品质有很详细的通晓。

3、总结种种页面包车型大巴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>

一.让文字不停地滚动

一.让文字不停地滚动

参考资料:

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

    1 赞 1 收藏
    评论

图片 1

  <MAEvoqueQUEE>滚动文字</MAEscortQUEE>

  <MASportageQUEE>滚动文字</MA陆风X八QUEE>

二.记下并出示网页的尾声修改时间

二.笔录并显示网页的末尾修改时间

  <script language=javascript>   document.write(“最后更新时间: “

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

  • document.lastModified + “”)   </script>

3.关闭当前窗口

3.闭馆当前窗口

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

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

四.5秒后关门当前页

四.5秒后关闭当前页

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

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

五.二秒后载入内定网页

5.2秒后载入钦点网页

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

  <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>

  <Script Language=”javascript”>   function bookmarkit()   {
  window.external.addFavorite(”)   }
  if (document.all)document.write(‘<a href=”#”
onClick=”bookmarkit()”>插足收藏夹</a>’)   </Script>

7.让超链接不突显下划线

7.让超链接不显得下划线

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

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

八.禁止鼠标右键的动作

捌.禁止鼠标右键的动作

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

  <Script Language = “javascript”>   function click() { if
(event.button==贰||event.button==三)   {   alert(‘禁止鼠标右键’);
  }   document.onmousedown=click // –>   </Script>

玖.装置该页为首页

九.装置该页为首页

  <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=”二”
face=”钟鼓文”>设为首页</font></a>
  </body>

  <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>

拾.回看日倒计时

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图