获取跨域脚本标记的HTML5性能对象

时间:2014-10-16 15:20:55

标签: javascript html5 cross-domain

我想知道,因为现代浏览器中的window.performance对象中有domainLookupStart和domainLookupEnd属性,有没有办法知道托管在第三方网站上的脚本的域查找时间?或者这次是否已包含在给定的domainLookupStart和domainLookupEnd中?

1 个答案:

答案 0 :(得分:1)

您可以使用Resource Timing API

记录当前文档中所有资源的DNS查找时间:

var resourceTimings = performance.getEntriesByType('resource');
  resourceTimings.forEach(function(resource) {
  console.log(resource.name + ' ' + (resource.domainLookupEnd - resource.domainLookupStart));
})

获取单个命名资源的所有统计信息:

var jQueryTiming = performance.getEntriesByName("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")

示例结果:

[
  {
    "responseEnd": 436.0980000055861,
    "responseStart": 434.55200002063066,
    "requestStart": 332.36200001556426,
    "secureConnectionStart": 0,
    "connectEnd": 332.30700000422075,
    "connectStart": 332.18300002045,
    "domainLookupEnd": 332.18300002045,
    "domainLookupStart": 320.040999999037,
    "fetchStart": 316.93600001744926,
    "redirectEnd": 0,
    "redirectStart": 0,
    "initiatorType": "script",
    "duration": 119.16199998813681,
    "startTime": 316.93600001744926,
    "entryType": "resource",
    "name": "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
  }
]

推荐阅读:http://www.sitepoint.com/introduction-resource-timing-api/

当前的浏览器兼容性:http://caniuse.com/#feat=resource-timing