2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > JS 监听手机返回键

JS 监听手机返回键

时间:2020-02-23 03:40:31

相关推荐

JS 监听手机返回键

JS监听手机返回键,需要用些前端的“奇技淫巧”。

核心代码如下:

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');

}

//监听手机返回键

if (window.history && window.history.pushState) {

$(window).on('popstate', function () {

var hash = window.location.hash;

if (hash === '') {

goForback();//返回

window.history.pushState('forward', null, 'homePage.html');//注意homePage.html是返回路径(继续监听)

}

});

window.history.pushState('forward', null, '');//注意homePage.html是返回路径

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。