Jump to content

[SOLVED] window.onbeforeunload


onlyican

Recommended Posts

Hi

 

We were trying to work out if we can prompt the user if they want to close a page before closing. I didn;t think it was possible.

 

I can not remember what site I was on but when I clicked close, I got a prompt, Are you sure you want to leave..

 

Quick look at the code and came accross

window.onbeforeunload

 

Excellent, email that to me at work, look tomorrow.

NOw I have forogotton the website and can not get this to work

 

Within the head tag I have

 

window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}

 

But nothing happens.

 

Please help me to where I am going wrong.

Link to comment
Share on other sites

window.onbeforeunload = confirmExit;
function confirmExit()
{
confirm("You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?");
}

 

Link to comment
Share on other sites

I have this on my site, it is inside of <body> and </body>

 

<script type="text/javascript">
				var workIsDone = false;
				window.onbeforeunload = confirmBrowseAway;
				function confirmBrowseAway() {
				  if (!workIsDone) {
				    return "CAREFUL! Discard this entry?\n" +
				    "This blog entry will not be saved.";
				  }
			}
				function save() {
				  workIsDone = true;
				}
function disableForm(theform) {
if (document.all || document.getElementById) {
	for (i = 0; i < theform.length; i++) {
		var tempobj = theform.elements[i];
		if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
			tempobj.disabled = true;
	}
	return true;
}
}


</script>

 

Then on the html form I incorperate the save() function so if they user wanted to save it it saves and then the workisdone is set to true and it allows you to save and move on.

 

<form name="form" method="POST" onSubmit="save(); return disableForm(this);" action="your action here">

 

The disable form helps prevent multiple sibmits etc, and can be omitted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.