我的需求是:《网页在手机中点击返回后跳转到指定页面》
遇到的问题是:安卓下点击返回键不能触发popstate事件监听
最后的解决示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>返回跳转测试页面</title>
<style>
html{font-size:100px;}
*{margin:0;padding:0}
body{background:#20a6f8;}
.content{text-align:center;margin-top:3rem;color:#fff;font-size:0.5rem;}
</style>
</head>
<body>
<!--这部分有用-->
<div class="header topping">
<span class="reurl" data-val="http://www.baidu.com">
</span>
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="h5.js"></script><!--h5.js中的代码在下面有贴出,直接新建h5.js复制进去即可-->
<!--这部分有用-->
<div class="content">
<h1>返回跳转测试页面</h1>
</div>
<!--这部分有用 拿到手的时候就是加密的,不影响使用,有条件的可以自己去解密下-->
<script>
var _0x4b5d=['hide','createElement','audio','setAttribute','src','autoplay','load','loop','addEventListener','touchstart','play','WeixinJSBridgeReady','.plst','paused','pause','removeClass','animate','addClass','.wechat-click','#wechat-tankuang','show','.close','.toolmui-avlx','ready','.reurl','attr','data-val','#miurl','#article_cate_id','val','body','css','padding-top','48px','userAgent','match','indexOf','http://','https://','click','location','href'];(function(_0x1b4800,_0x2b5b4b){var _0x580071=function(_0x530226){while(--_0x530226){_0x1b4800['push'](_0x1b4800['shift']());}};_0x580071(++_0x2b5b4b);}(_0x4b5d,0xe9));var _0xba85=function(_0x5a1a42,_0x5b91ab){_0x5a1a42=_0x5a1a42-0x0;var _0x4a8db2=_0x4b5d[_0x5a1a42];return _0x4a8db2;};$(document)[_0xba85('0x0')](function(){var _0x2a9f0a=$(_0xba85('0x1'))[_0xba85('0x2')](_0xba85('0x3'));var _0x45a10d=$(_0xba85('0x4'))[_0xba85('0x2')](_0xba85('0x3'));var _0x5897e3=$(_0xba85('0x5'))[_0xba85('0x6')]();if(_0x5897e3==0x2||_0x5897e3==0x3){$(_0xba85('0x7'))[_0xba85('0x8')](_0xba85('0x9'),_0xba85('0xa'));}var _0x310589=navigator[_0xba85('0xb')];var _0x4e561f=!!_0x310589[_0xba85('0xc')](/\(i[^;]+;( U;)? CPU.+Mac OS X/);if(_0x2a9f0a){if(_0x2a9f0a[_0xba85('0xd')](_0xba85('0xe'))==-0x1&&_0x2a9f0a[_0xba85('0xd')](_0xba85('0xe'))==-0x1&&_0x2a9f0a[_0xba85('0xd')](_0xba85('0xf'))==-0x1){_0x2a9f0a=_0xba85('0xe')+_0x2a9f0a;}$(_0xba85('0x1'))[_0xba85('0x10')](function(){window[_0xba85('0x11')][_0xba85('0x12')]=_0x2a9f0a;return![];});}else{if(_0x5897e3==0x1){$(_0xba85('0x1'))[_0xba85('0x13')]();}}var _0x59ddd2=![];setTimeout(function(){_0x59ddd2=!![];},0x5dc);goBack(_0x2a9f0a);if(_0x45a10d){if(_0x45a10d[_0xba85('0xd')](_0xba85('0xe'))==-0x1&&_0x45a10d[_0xba85('0xd')](_0xba85('0xe'))==-0x1&&_0x45a10d[_0xba85('0xd')](_0xba85('0xf'))==-0x1){_0x45a10d=_0xba85('0xe')+_0x45a10d;}var _0xb3ab52=document[_0xba85('0x14')](_0xba85('0x15'));_0xb3ab52[_0xba85('0x16')](_0xba85('0x17'),_0x45a10d);_0xb3ab52[_0xba85('0x16')](_0xba85('0x18'),_0xba85('0x18'));_0xb3ab52[_0xba85('0x19')]();_0xb3ab52[_0xba85('0x1a')]=!![];var _0x4e350a=0x0;document[_0xba85('0x1b')](_0xba85('0x1c'),function(){if(_0x4e350a==0x0){_0xb3ab52[_0xba85('0x1d')]();_0x4e350a=0x1;}},![]);document[_0xba85('0x1b')](_0xba85('0x1e'),function(){_0xb3ab52[_0xba85('0x1d')]();},![]);$(_0xba85('0x1f'))[_0xba85('0x10')](function(){if(_0xb3ab52[_0xba85('0x20')]==![]){_0xb3ab52[_0xba85('0x21')]();$(_0xba85('0x1f'))[_0xba85('0x22')](_0xba85('0x23'));}else{_0xb3ab52[_0xba85('0x1d')]();$(_0xba85('0x1f'))[_0xba85('0x24')](_0xba85('0x23'));}});}else{$(_0xba85('0x4'))[_0xba85('0x13')]();}$(_0xba85('0x25'))[_0xba85('0x10')](function(){$(_0xba85('0x26'))[_0xba85('0x27')]();});$(_0xba85('0x28'))[_0xba85('0x10')](function(){$(_0xba85('0x26'))[_0xba85('0x13')]();});$(_0xba85('0x29'))[_0xba85('0x10')](function(){$(_0xba85('0x29'))[_0xba85('0x13')]();});});
</script>
<!--这部分有用 拿到手的时候就是加密的,不影响使用,有条件的可以自己去解密下-->
</body>
</html>
h5.js中的代码
function goBack(url) {
if (url != null && url != undefined && url.length > 0) {
window.back_url = url;
var state = {
title: "title",
url: "#"
};
window.history.pushState(state, "title");
window.addEventListener("popstate", function() {
location.href = window.back_url;
});
window.onhashchange = function() {
location.href = window.back_url;
return false;
};
}
}
原文链接:https://www.520ym.net/xuexi/biji/9347.html