Skip to content

Instantly share code, notes, and snippets.

@iahu
Created Sep 22, 2015
Embed
What would you like to do?
篡改“浏览器后退按钮”历史纪录 demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
<style>
</style>
</head>
<body>
<p>demo.html</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style>
.test { font: 18px/1.5 微软雅黑, 黑体,arial, sans-serif;}
</style>
</head>
<body>
<h3 class="test">篡改“浏览器后退按钮”历史纪录 demo</h3>
<p>点击下面链接几次后,再点击浏览器后退按钮试试</p>
<a href="#!1">hash #!1</a>
<a href="#!2">hash #!2</a>
<script>
var inPage = false;
document.body.addEventListener('mouseover', function(e) {
inPage = true;
});
document.body.addEventListener('mouseenter', function(e) {
inPage = true;
});
document.body.addEventListener('mouseleave', function(e) {
inPage = false;
});
window.addEventListener('popstate', function(e) {
if ( !inPage && /#!\d/g.test( location.hash ) ) {
location.replace('demo.html');
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment