9

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?

Mar
  • 7,765
  • 9
  • 48
  • 82
  • 1
    You can get the Date from the HTTP header. See this post: http://stackoverflow.com/questions/27968368/getting-date-from-http-header-response – Fals Feb 18 '15 at 17:15
  • 1
    You can get timings from `window.performance.timing`. Have a look at [this](http://stackoverflow.com/questions/14341156/calculating-page-load-time-in-javascript#14878493) for more details :) – Arkantos Feb 18 '15 at 17:16
  • 1
    Arkantos beat me to it. It looks like that API was removed in iOS 8.1 though! That's pretty disappointing. – Katana314 Feb 18 '15 at 17:17
  • @Katana314 So that means iPhones + iPads don't have that info, but PC browsers do? – Mar Feb 18 '15 at 17:22
  • @Katana314.. thanks for mentioning that :) As you can see [here](http://caniuse.com/#feat=nav-timing), it's removed in iOS 8.1 but there're still good range of browsers that support it – Arkantos Feb 18 '15 at 17:23
  • iPhones & iPads with chrome browsers still do ;) – Arkantos Feb 18 '15 at 17:23

1 Answers1

6

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.

joews
  • 29,767
  • 10
  • 79
  • 91