5

In JavaScript it is possible to wait for onLoad which may be used to specify the time of page loading completion.

Every HTTP response is sent along with Date: header which contains the time server sent a response.

Is there a way in JavaScript to get the time of page started loading? Something similar to response Date: header.

It would be useful for cases when JavaScript is injected into page after some delay.

nilfalse
  • 2,380
  • 2
  • 19
  • 16

5 Answers5

9

new Date(performance.timing.connectStart) in chrome, firefox, IE9, etc (caniuse)

demo

console.log(new Date(performance.timing.connectStart));
Flimm
  • 136,138
  • 45
  • 251
  • 267
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • As a side bonus I learned that: *The window.performance.navigation object stores two attributes that can be used to know if a page load is triggered by a redirect, back/forward button or normal URL load.* – Esailija Jul 02 '12 at 15:12
1

Try storing a value from var d= new Date(); var requested = d.getTime(); and execute it when the page loads.

Polyov
  • 2,281
  • 2
  • 26
  • 36
0
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var loadDate;
    </script>
</head>
<body onload="loadDate=new Date();">
    <button onclick="alert(loadDate);">show load Date</button>
</body>
</html>
Michael Besteck
  • 2,415
  • 18
  • 10
0

Update 2023

As performance.timing.connectStart is deprecated, I recommend:

window.performance?.timeOrigin

Here is my code to get something bullet-proof:

    const sessionStartDateTime = window.performance?.timeOrigin ?? window.performance?.timing?.connectStart ?? Date.now();

Sources:

Hope ir helps :)

Juanmabs22
  • 1,194
  • 9
  • 10
-1

I would just pass a timestamp from the server-side script to the browser via a cookie or inline JS. E.g. in PHP:

<script>
var timestamp = new Date(<?php echo time(); ?>);
</script>
Lèse majesté
  • 7,923
  • 2
  • 33
  • 44