我尝试在两个本地HTML页面中测试document.referrer
的使用,但它不起作用。
这是我的代码:
HTML1:
<body>
<button type="button" name="button" onclick="go()">go!</button>
<a href="123.html">go with a</a>
</body>
<script type="text/javascript">
function go() {
location.assign('123.html')
}
</script>
HTML2(123.html(:
<body>
<button type="button" name="button" onclick="gopre()">back</button>
</body>
<script type="text/javascript">
function gopre() {
location.assign(document.referrer);
//console.log(document.referrer);//it's null
}
</script>
document.referrer
在本地工作吗?我应该如何使用它?
也许不能。
document.referrer
将由浏览器分配,并由浏览器存储。
解决方案是您可以启动一个Quick Start
Web 服务器。
现在我知道为什么它不能在本地工作了,因为它依赖于HTTP请求来工作,
即使在从HTTPS页面跳转的HTTP页面中,您也不能使用document.referrer
!