Reputation: 2419
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.
Upvotes: 6
Views: 4964
Reputation: 1300
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 :)
Upvotes: 0
Reputation: 140234
new Date(performance.timing.connectStart)
in chrome, firefox, IE9, etc (caniuse)
console.log(new Date(performance.timing.connectStart));
Upvotes: 12
Reputation: 2423
<!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>
Upvotes: 0
Reputation: 8045
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>
Upvotes: -1
Reputation: 2321
Try storing a value from var d= new Date(); var requested = d.getTime();
and execute it when the page loads.
Upvotes: 1