[英]Javascript History.pushState forward and backward history
I need an event for when the user clicks on the browser back and forward buttons. 当用户单击浏览器的后退和前进按钮时,我需要一个事件。 I have this piece of code that works, however it executes regardless of which button I click (backward & forward).
我有一段有效的代码,但是无论我单击哪个按钮(后退和前进),它都会执行。 How can I handle the backward and forward button clicks separately?
如何分别处理后退和前进按钮单击?
The code: 编码:
if ( window.history && window.history.pushState ) {
window.history.pushState( 'forward', null, './#forward' );
$( window ).on( 'popstate', function() {
alert( 'Hello back/forward button!' );
} );
}
Is it also possible to hide the #forward
string from the URL? 是否还可以从URL中隐藏
#forward
字符串?
Use the direction: 使用说明:
$(window).on('navigate', function (event, data) {
var direction = data.state.direction;
if (direction == 'back') {
// do something
}
if (direction == 'forward') {
// do something else
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.