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

一个简单的demo,大家可以看看:

测试demo




原文地址:https://www.cnblogs.com/prolovecui/p/6687392.html