最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

http redirect - Stop page redirection using javascript without warning or asking - Stack Overflow

programmeradmin4浏览0评论

This answer tells how to stop redirection:

<script language="JavaScript" type="text/javascript">
    //<![CDATA[
        window.onbeforeunload = function(){
            return 'Are you sure you want to leave?';
        };
    //]]>
</script>

This codes works, but asks an user whether to stay on the page or redirect. Can the redirect be stopped without any type of alert or asking?

This answer tells how to stop redirection:

<script language="JavaScript" type="text/javascript">
    //<![CDATA[
        window.onbeforeunload = function(){
            return 'Are you sure you want to leave?';
        };
    //]]>
</script>

This codes works, but asks an user whether to stay on the page or redirect. Can the redirect be stopped without any type of alert or asking?

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Oct 28, 2014 at 19:55 тнє Sufiтнє Sufi 6342 gold badges10 silver badges23 bronze badges 2
  • Are you sure that it will be user friendly? Visitor would never be able to leave your page – mandza Commented Oct 28, 2014 at 19:58
  • That did not work on my mind. – тнє Sufi Commented Oct 28, 2014 at 19:59
Add a ment  | 

1 Answer 1

Reset to default 5

No, it cannot. If that were allowed, a malicious page could permanently prevent someone from leaving it. Therefore, the user must be allowed to decide.

If you are leaving from an event handler though, you can usually return false to stop it from happening.

发布评论

评论列表(0)

  1. 暂无评论