Save / Discard Changes

I made a JQuery script that prevents the user from leaving a edit screen if changes are made to it, not using the "Save" button.
I tested it with communications and person summary but it should also work with other edit screens.
It´s a bit dirty, because if you make a change and put it back to original, you also get a message.

Custom Content:

<script type="text/javascript" src="../jQuery/jquery-1.7.1.min.js"></script>
<script>

// clean/dirty variable
var formHasChanged = false;

$(document).ready(function()

{
$("select, input, textarea").change(function()
{
// something has changed
formHasChanged = true;
});

// select "save" button and add a click function to clear the variable if user clicks "save"
$("a[ACCESSKEY='S']").click(function()
{
formHasChanged = false;
});
});

//function to prevent user from leaving screen
window.onbeforeunload = function (e)

{
if (!formHasChanged)
return;

var e = e || window.event,
str = 'Close without saving?';

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

// For Safari
return str;
}
</script>

I managed to identify the save-button by the attribute "ACCESSKEY='S'". I need this button to attache the onclick function that clears the dirty/clean variable. This should work for german and english languages but not if the attribute is any other.

Does someone has a better solution for this?