我的网站使用 asual address plugin用于实现基于哈希的 URL。
在其中一个页面上,我放置了一个返回 链接。我希望单击此链接时出现以下行为。
$('#back_link').click(function(){ If there is any entry in history it should do history.back() If no entry in history, load a default page. })
这可能吗?
注意:我可以在 Firefox 中执行此操作(点击链接前后 window.location.hash 不同)。
但这不适用于 chrome。
我也尝试使用 history.length 但没有成功。
最佳答案
如果您要通过链接转到该页面,您可以检查 document.referrer 并调用 history(-1)(如果存在),但如果用户输入地址,即使有历史,它也会返回空。我认为您不能只获取无关浏览器历史记录的 url(甚至存在),因为那会是一个安全问题。
关于JavaScript history.back() 有条件地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3685354/