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

javascript - How to prevent "Are you sure you want to navigate away from this page?" alert on form submission?

programmeradmin4浏览0评论

I have a simple html form with few fields in it.

I wanted to make sure that user gets an alert message when he is trying to leave the page. So I added the script on the page which will run whenever user is trying to navigate away from this page.

But I am also getting this alert message when user is trying to submit the form. I dont want this alert message when user is saving the form.

Please help.

Below is some sample code.

<html>
    <head>

    <script>
        function verifyexit() { return 'Are you sure ?'; }           window.onbeforeunload = verifyexit;
    </script>       
</head>
<body>
        Go to <a href="b.html"> another page </a> 
        <form action="c.html">
            name : <input type="text" name="name" value=""/> <br>
            email : <input type="text" name="email" value=""/> <br>
            <input type="submit" value="save"/>
        </form>
</body> 

I have a simple html form with few fields in it.

I wanted to make sure that user gets an alert message when he is trying to leave the page. So I added the script on the page which will run whenever user is trying to navigate away from this page.

But I am also getting this alert message when user is trying to submit the form. I dont want this alert message when user is saving the form.

Please help.

Below is some sample code.

<html>
    <head>

    <script>
        function verifyexit() { return 'Are you sure ?'; }           window.onbeforeunload = verifyexit;
    </script>       
</head>
<body>
        Go to <a href="b.html"> another page </a> 
        <form action="c.html">
            name : <input type="text" name="name" value=""/> <br>
            email : <input type="text" name="email" value=""/> <br>
            <input type="submit" value="save"/>
        </form>
</body> 

Share Improve this question asked Jun 25, 2012 at 4:56 ajmajm 13.2k59 gold badges168 silver badges241 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

stackoverflow reference

To turn it on:

window.onbeforeunload = "Are you sure you want to leave?";

To turn it off:

window.onbeforeunload = null;

Bear in mind that this isn't a normal event - you can't bind to it in the standard way.

To check for values? That depends on your validation framework.

In jQuery this could be something like (very basic example):

$('input').change(function() {
    if( $(this).val() != "" )
        window.onbeforeunload = "Are you sure you want to leave?";
});

With JQuery this stuff is pretty easy to do. Since you can bind to sets.

Its NOT enough to do the onbeforeunload, you want to only trigger the navigate away if someone started editing stuff.

To make this work in Chrome and Safari, you would have to do it like this

window.onbeforeunload = function (e) {
    e = e || window.event;

    var msg = "Sure you want to leave?";

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = msg;
    }

    // For Safari
    return msg;
};


This is the general rule in reference

window.onbeforeunload = function (e) {
  e = e || window.event;

  // For IE<8 and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Chrome, Safari, IE8+ and Opera 12+
  return 'Any string';
};

reference: https://developer.mozilla/en/DOM/window.onbeforeunload

Try this:

<form action="c.html" onsubmit="window.onbeforeunload=function(){}">

Try this:

$(window).bind("beforeUnload", verify);
$("form").submit(function (){
    $(window).unbind("beforeUnload");
};

Try this: http://cfsilence./blog/client/index.cfm/2009/10/12/jQuery-Method-To-Prompt-A-User-To-Save-Changes-Before-Leaving-Page

I added another bit to the function:

 $('#submitButton').click(function () {
        isDirty = false;
});

Just so the form doesn't ask for validation when you want to submit it :)

I hope this helps

Note: You could also do something like $("input[type=submit]") if you don't like to depend on a mon naming (else your submit button needs to be called submitButton)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论