Warn User When Navigating from Page with Unsaved Changes
<p>I'm creating an ASP.NET website and I want to implement logic to warn the user when they are navigating away from a page they've edited.</p>

<p>I found quite a bit of info on the web, although most of it seems quite outdated. Note that I still have a lot to learn about jQuery.</p>

<p>The following code displays the message as expected.</p>

<pre><code>window.onbeforeunload = function () {
    return 'You have unsaved changes!';
}
</code></pre>

<p>However, the following code--which is supposed to be equal to the code above only when a change is made--does not work. No warning is ever displayed.</p>

<pre><code>$('input').change(function () {
    window.onbeforeunload = function () { return "Your changes have not been saved?" };
});
</code></pre>

<p>Can someone say why the second snippet doesn't work? Or perhaps you can point me to a reference that is more recent.</p>
