復(fù)制代碼 代碼如下:
/* 獲取渲染開始的時(shí)間戳, 保存在數(shù)組PAGE_SPEED_TIME中 */
<html><script type="text/javascript">/*tag*/PAGE_SPEED_TIME = [new Date().getTime()];</script><head>
......
</head>
復(fù)制代碼 代碼如下:
......
/* 頁(yè)面最末端,計(jì)算頁(yè)面加載耗用的時(shí)間 */
</body>
<script src="http://ossweb-img.qq.com/images/js/pagespeed/page_speed_v2.js"></script></html>
下面來(lái)分析一下page_speed_v2.js文件的內(nèi)容。對(duì)這個(gè)腳本進(jìn)行重新格式化,以便更加方便閱讀。
復(fù)制代碼 代碼如下:
/* 執(zhí)行匿名函數(shù)構(gòu)建對(duì)象PageSpeed */
;(function() {
PageSpeed = {};/* 這里沒(méi)有 var 關(guān)鍵字, 相當(dāng)于引用的是 window.PageSpeed */
/**
* 綁定createScript方法
*
* @param String a script標(biāo)簽的src屬性的值
* @param String b script標(biāo)簽的id屬性的值
*/
PageSpeed.createScript = function(a, b) {
var c = null;
if (document.getElementById(b)) { /* script標(biāo)簽已經(jīng)存在 */
c = document.getElementById(b)
} else { /* 不存在, 創(chuàng)建script標(biāo)簽 */
c = document.createElement('script');
var d = null;
if (document.getElementsByTagName) {
d = document.getElementsByTagName('head')[0] || document.documentElement;
} else {
d = document.documentElement;
}
d.insertBefore(c, d.firstChild); /* 調(diào)用insertBefore方法, 將新創(chuàng)建的script標(biāo)簽插入為第一個(gè)子元素 */
}
/* 這里的花括號(hào)起到代碼片段分組功能 */
{
c.setAttribute('type', 'text/html');
c.setAttribute('style', 'display:none;');
c.setAttribute('charset', 'gb2312');
c.setAttribute('id', b);
c.setAttribute('src', a);
}
return c; /* 返回創(chuàng)建成功的script標(biāo)簽元素 */
};
/**
* 綁定 submitDataForPageSpeed 方法
*
* @param Object a Map對(duì)象
*/
PageSpeed.submitDataForPageSpeed = function(a) {
var b = {
'name': '',
'rate': 0
};
b.name = a.name;
b.rate = a.rate;
/* a.PAGE_SPEED_TIME數(shù)組的最后一個(gè)元素減去第一個(gè)元素 */
b['1'] = a.PAGE_SPEED_TIME[a.PAGE_SPEED_TIME.length - 1] - a.PAGE_SPEED_TIME[0];
for (var i = 1; i < a.PAGE_SPEED_TIME.length - 1; i++) {
b[(i + 1) + ''] = a.PAGE_SPEED_TIME[i] - a.PAGE_SPEED_TIME[0]
}
/* 提交速度測(cè)試結(jié)果的目的腳本 */
var c = 'http://pagespeed-ied.qq.com/r.cgi';
/* 遍歷 b 數(shù)組的元素, 構(gòu)建 query string */
var d = [];
for (var p in b) {
d.push(p + '=' + b[p])
}
c += '?' + d.join('&');
/* 創(chuàng)建script標(biāo)簽提交測(cè)速結(jié)果 */
PageSpeed.createScript(c, 'submitForPageSpeed')
};
/**
* 綁定 defaultSubmit 方法
*
*/
PageSpeed.defaultSubmit = function() {
var a = '';
try {
a = location.host
} catch(e) {}
var b = {
'name': a,
'rate': 1,
'PAGE_SPEED_TIME': []
};
if (!b.name) {
return
}
if (! (typeof(PAGE_SPEED_TIME) == 'object' && PAGE_SPEED_TIME instanceof Array)) {
return
}
/* 獲取新的時(shí)間戳 */
PAGE_SPEED_TIME.push(new Date().getTime());
b.PAGE_SPEED_TIME = PAGE_SPEED_TIME;
/*
* 沒(méi)看懂這里為啥需要一個(gè)新的變量 aaa, 直接使用 a 不可以么?
* try ... catch 結(jié)構(gòu)中使用的卻是一個(gè)新的變量 aaaa 但后面卻從未使用, 為何? 是否應(yīng)該是 aaa?
*/
var aaa = '';
try {
aaaa = location.host
} catch(e) {}
var c = Math.floor(Math.random() * 10000);
if (aaa == "ktv.qq.com" || aaa == "ttd.qq.com" || aaa == "tian.qq.com" || aaa == "sura.qq.com" || aaa == "gw.tnt.qq.com" || aaa == "007.qq.com") {
c = Math.floor(Math.random() * 1000);
}
/* 隨機(jī)概率提交數(shù)據(jù) */
if (c <= b.rate * 1) {
PageSpeed.submitDataForPageSpeed(b);
}
};
/**
* 綁定submit方法(用于CDN測(cè)速?)
*
* @param String a 名稱
*/
PageSpeed.submit = function(a) {
var b = PageSpeed.cdn_page_speed_submitData;
var c = {};
for (var p in b) {
c[p] = b[p]
}
c.name = a || c.name;
PageSpeed.submitDataForPageSpeed(c);
}
})();
try {
/* 1秒后嘗試提交數(shù)據(jù) */
setTimeout(function() {
PageSpeed.defaultSubmit()
},
1000);
} catch(e) {}
/* 最后這段注釋, 第二段是32位16進(jìn)制數(shù)字, 應(yīng)該是類似于 ETag 用于標(biāo)記內(nèi)容版本的 */
/* |xGv00|ca82276cd78ac911d3d4310ba1408236 */
新聞熱點(diǎn)
疑難解答
圖片精選