我需要检测用户是否点击了返回按钮。
为此,我正在使用
window.onbeforeunload = function (e) {
}
如果用户点击返回按钮,它是有效的。但是,如果用户点击浏览器的 F5 或刷新按钮,则此事件也会被触发。我该如何解决?
我需要检测用户是否点击了返回按钮。
为此,我正在使用
window.onbeforeunload = function (e) {
}
如果用户点击返回按钮,它是有效的。但是,如果用户点击浏览器的 F5 或刷新按钮,则此事件也会被触发。我该如何解决?
就 AJAX 而言...
在使用大多数使用 AJAX 导航页面特定部分的 Web 应用程序时,按返回按钮是一个巨大的问题。我不认为“必须禁用按钮意味着你做错了什么”,实际上,不同领域的开发人员长期以来一直遇到这个问题。这是我的解决方案:
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
// Detect and redirect change here
// Works in older Firefox and Internet Explorer 9
// * it does mess with your hash symbol (anchor?) pound sign
// delimiter on the end of the URL
}
else {
ignoreHashChange = false;
}
};
}
}
history.go(-2);
。如果浏览器不支持 "onbeforeunload",请尝试以下方法:
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
$(window).on('popstate', function() {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
alert('Back button was pressed.');
}
}
});
window.history.pushState('forward', null, './#forward');
}
});
window.history.pushState
是对象 window.history
的一个子级/方法。仅测试该函数是否足够吗? - Oliver Schafeldwindow.history.pushState
只有(或首先)在浏览器实现历史记录 API 的情况下才能抛出 Cannot read property 'pushState' of undefined
。因此,这两个测试是必需的,并且需要按照特定的顺序进行。 - Nico Prat!!window.history.madeupproperty
的结果是 false
。所以我猜 if(!!window.history.pushState)
可能会起作用。但是我无法得到 IE9(http://caniuse.com/#feat=history) 进行适当的测试。但是简单地检查 !!window.notexistent.madeup
会抛出“无法读取属性...”的错误。因此,确实需要 if (window.history && window.history.pushState)
,而且顺序也很重要。 - Oliver Schafeldalert('...')
行之后添加了最后一行window.history.pushState('forward', null, './#forward')
。这就解决了问题......在后退按钮点击时没有刷新或任何明显的效果。 - Nowdeenwindow.onbeforeunload = function (e) {
var e = e || window.event;
var msg = "Do you really want to leave this page?"
// For Internet Explorer and Firefox
if (e) {
e.returnValue = msg;
}
// For Safari / Chrome
return msg;
};
window.onload = function () {
if (typeof history.pushState === "function")
{
history.pushState("jibberish", null, null);
window.onpopstate = function ()
{
history.pushState('newjibberish', null, null);
// Handle the back (or forward) buttons here
// Will NOT handle refresh, use onbeforeunload for this.
};
}
它可以工作,但我必须在Chrome中创建一个cookie来检测我第一次进入页面,因为当我没有通过cookie进行控制进入页面时,浏览器会执行返回操作而不是点击任何返回按钮。
if (typeof history.pushState === "function")
{
history.pushState("jibberish", null, null);
window.onpopstate = function () {
if (((x = usera.indexOf("Chrome")) != -1) &&
readCookie('cookieChrome') == null)
{
addCookie('cookieChrome', 1, 1440);
}
else
{
history.pushState('newjibberish', null, null);
}
};
}
非常重要的是,history.pushState("jibberish", null, null);
会复制浏览器历史记录。
我该如何解决这个问题?
由于后退按钮是浏览器的功能,更改默认功能可能会很困难。不过,有一些解决方法。请查看本文:
通常,需要禁用后退按钮是编程问题/缺陷的一个很好的指标。我建议寻找替代方法,例如设置会话变量或存储表单是否已提交的cookie。
window.performance && window.performance.navigation.type == 2
。 - Master DJon