OK, so why would the page/form refresh while still being "dirty"? I can see the user clicking the refresh button, but warn the user then too, because I would assume that the refresh could refresh the page to show the older data values, and therefore lose your unsaved data.
Meaning I am thinking the way it is operating is the way you should want it. But I could be clueless, it wouldn't be the first time.
I have a form where the user can add favorite forms to their profile. The users insist on having a save and cancel button, so I can't persist the data immediately. Each time they add a new favorite, or remove one, the screen is updated to display this change(so the page gets refreshed) but the data is not saved until they click on the save button or the changes are reverted back if they click on the cancel button. But there are other links the user can click on that will navagate away from the page.
This gets a but more complicated in that there is a parent page, that holds several iframes with other pages. And it's the iframe pages where the data can be updated.
So the onBeforeUnload event would be the best option if I could only determine what the current page is and what the target page is.