简体   繁体   中英

How long ago did I load a page?

Is there a way to get how long ago a page was loaded using javascript, without having first recorded the time to a javascript variable on page load? I'm hoping to create a scriptlet (javascript bookmark) that I can run on any web page and have it output how much time has passed since the browser loaded the page.

All of the "time spent on a page" solutions I've found so far rely on recording the time when the page first loads, which requires either access to modify a site, or a browser plugin. Is there no document property that stores when the page was loaded (started, finished, etc.) which can be accessed in javascript?

You could use window.performance.timing.loadEventEnd :

> performance.timing.loadEventEnd
1424280020354

window.performance.timing provides timestamps for the following page load lifecycle events, in order:

  • navigationStart
  • unloadEventStart
  • unloadEventEnd
  • redirectStart
  • redirectEnd
  • fetchStart
  • domainLookupStart
  • domainLookupEnd
  • connectStart
  • connectEnd
  • secureConnectionStart
  • requestStart
  • responseStart
  • responseEnd
  • domLoading
  • domInteractive
  • domContentLoadedEventStart
  • domContentLoadedEventEnd
  • domComplete
  • loadEventStart
  • loadEventEnd

Source: Navigation Timing API on MDN

The Navigation Timing API is currently supported by most modern browsers. Exceptions: Safari < 8 on OSX, Safari on iOS and Opera Mini. - caniuse.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM