开发者

How to prevent users from moving pages?

I know somethings like this;

<script>
function out()  {
    alert('No, stay here.');
    //What to do?...
}
</script>

<body onUnload="out()">
<input />
</body>

I got some response from other community

<script>
function out()  {
    alert('No开发者_StackOverflow中文版, stay here.');
    location.href='thisPage.html';
}

But this will reload the page. I want to prevent users from moving pages without reloading page. Thank you


You can use the onbeforeunload event and take the steps whatever you want:

window.onbeforeunload = function(){
  // whatever you want to do now
};

With this, you can redirect users to some other page if they try to move away from current page or any action you want to perform there.


Don't ever do this: there's a reason a user wants to leave the page, so don't force him/her to stay. A better option would be to use onbeforeunload to ask for confirmation before leaving the page.


Try return false; instead of location.href='thisPage.html';


Instead of returning nothing, add

return false;

to the end, that should work


:)

function out()  {
    alert('No, stay here.');
    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜