【问题标题】:Get HTML5 performance object for cross-domain script tag获取跨域脚本标签的 HTML5 性能对象
【发布时间】:2014-10-16 23:43:03
【问题描述】:

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

【问题讨论】:

标签: javascript html cross-domain


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2014-12-05
    • 2017-12-03
    相关资源
    最近更新 更多