如何检查用户是否可以返回浏览器历史记录 [英] How to check if the user can go back in browser history or not
问题描述
我想使用JavaScript来查看是否有历史记录,我的意思是如果浏览器上有后退按钮可用。
I want using JavaScript to see if there is history or not, I mean if the back button is available on the browser or not.
推荐答案
简答:你不能。
从技术上讲,有一种准确的方法,就是检查财产:
Technically there is an accurate way, which would be checking the property:
history.previous
但是,它不起作用。问题在于,在大多数浏览器中,这被视为安全违规,通常只返回未定义。
However, it won't work. The problem with this is that in most browsers this is considered a security violation and usually just returns undefined.
history.length
属性其他人建议...
但是,长度不能完全发挥作用,因为它并不表示历史记录中的 where 。此外,它并不总是以相同的数字开头。例如,未设置为登录页面的浏览器从0开始,而使用朗读页面的另一个浏览器将从1开始。
Is a property that others have suggested...
However, the length doesn't work completely because it doesn't indicate where in the history you are. Additionally, it doesn't always start at the same number. A browser not set to have a landing page, for example, starts at 0 while another browser that uses a langing page will start at 1.
大部分时间都添加了一个调用链接:
Most of the time a link is added that calls:
history.back();
或
history.go(-1);
如果你不能回去,那么点击链接什么都不做。
and it's just expected that if you can't go back then clicking the link does nothing.
这篇关于如何检查用户是否可以返回浏览器历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!