您的位置:首页 > Web前端 > JavaScript

js获取浏览器返回按钮事件

2017-01-13 09:34 591 查看
前面要加上jquery比如<script src="style/jquery.min.js"></script>監聽事件
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('後退按鈕點擊');
}
}
});

window.history.pushState('forward', null, './#forward');
}
});

History 对象

History 对象包含用户(在浏览器窗口中)访问过的 URL。History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。

history.pushState(state, title, url)

将当前URL和history.state加入到history中,并用新的state和URL替换当前。不会造成页面刷新。state:与要跳转到的URL对应的状态信息。title:不知道干啥用,传空字符串就行了。url:要跳转到的URL地址,不能跨域。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js jquery 浏览器