我在我的ajax-app中使用pushStates从一个"页面"导航到另一个"页面".现在,我想看看我来自哪个页面.但document.referrer总是回归"".或者,当我从另一个页面(链接的位置)打开我的应用程序时,我从另一个页面获取了URL.
不应该这些线......
history.pushState({}, "Some title", "/some/valid/url/1");
history.pushState({}, "Some title", "/some/valid/url/2");
...产生这样的推荐人:
http://somedomain.com/some/valid/url/1
?
或换句话说:有没有办法相应地设置document.referrer,或至少重置为""?
注意:我正在寻找解决方案而不在某些变量中缓存以前的URL.我需要一些真正改变它的东西document.referrer,因为我无法改变依赖它的脚本.
简短回答:使用window.location而不是history.pushState
较长的答案:
document.referrer根据MDN:“如果用户直接导航到页面(不是通过链接,而是通过书签),则该值为空字符串”
直接操作history状态不会被视为跟随链接。window.location您可以通过更新(MDN )来模拟链接点击,这也会自动更新历史记录。
例如,加载带有https://github.com/kanaka/mal/. 然后一次输入以下一行(否则它们都在单个 JavaScript 执行上下文中运行,并且仅应用最后的位置更新)
console.log(history.length) // 2
console.log(document.referrer) // ""
window.location = "/kanaka/mal/tree/master/ada"
console.log(history.length) // 3
console.log(document.referrer) // "https://github.com/kanaka/mal"
window.location = "/kanaka/mal/tree/master/python"
console.log(history.length) // 4
console.log(document.referrer) // "https://github.com/kanaka/mal/tree/master/ada"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2253 次 |
| 最近记录: |