如何在本地测试文档引用?



我尝试在两个本地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 StartWeb 服务器。

现在我知道为什么它不能在本地工作了,因为它依赖于HTTP请求来工作,

即使在从HTTPS页面跳转的HTTP页面中,您也不能使用document.referrer

最新更新