A.html页面 > B.html页面 > A.html页面,计算从B.html页面回到A.html页面的耗时
A.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
var referrer = document.referrer; //注意:要放在服务器才有效果,在本地无效
if(referrer.indexOf('B.html')){
var myDate = new Date();
var time = myDate.getMilliseconds(); //获取当前毫秒数(0-999)
localStorage.setItem('time',time);
}else{
console.log(localStorage.getItem('time'));
}
</script>
</head>
<body>
<a href="B.html">
B文件
</a>
</body>
</html>
B.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
var myDate = new Date();
var time = (new Date()).valueOf();
localStorage.setItem('time',time);
</script>
</head>
<body>
<a href="A.html">
A文件
</a>
</body>
</html>
结果: