Prevent an accidental Back Browse
I have a client that just got the Apple Magic Mouse. We built 开发者_JAVA技巧a recipe website and she get very upset when she is inputting all the information for the recipe, then goes for her new mouse and accidentally scrolls to the right and it will do a Back Browse and all her info is lost. So she wants to warning/pop up to prevent this from happening on that page.
Any suggestions or point me to some JS that does this?
This is a basic 'warn before leave' script. When the user presses a key in the textarea, the user will be notified if (s)he is about to leave the page:
<script type="text/javascript">
var changes = false;
window.onbeforeunload = function(){
if(changes){
return "You're about to leave this page.";
}
};
</script>
<textarea onkeypress="window.changes=true">recipe here</textarea>
Alternatively, you can store the contents of the textfield in a variable when the page loads and compare it afterwards. This will take away some confusion when the visitor just press the arrow keys in the textarea without actually modifying it.
So she wants to warning/pop up to prevent this from happening on that page.
You can hook into the onBeforeUnload event of your browser and then show a confirmation message.
精彩评论