With Confirm() Can I Stop Back Button Action?

I have some text fields that users can type data into - how can I use JS confirm() to reload the same page if the user clicks on confirm's Cancel button? I want to prompt: Continuing off this page may cause loss of data - continue? When the user clicks on the browser back button they will loose whatever they typed in when they Fwd back to that web page - I want to at least let them know that if they choose to continue the Browser Back button action - in the confirm() Cancel how can I keep the same web page displayed?

Thanks...
rfreshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

superslamwichCommented:
Try this:

<body onBeforeUnload='return "Leaving the page will result in loss of data"'>

{Slam}
0
ZontarCommented:
> When the user clicks on the browser back button they will loose whatever they typed in when they Fwd back to that web page...

Another example of MSIE's superiority? ;)  Mozilla preserves form input when navigating forwards and backwards. Fortunately, it also doesn't do unbeforeunload events, so it shouldn't be affected by this. Not sure about Opera.
0
rfreshAuthor Commented:
Slam:

I tried that but it didn't work - is the syntax correct? I'm using IE 6.0

One other note: when I add that code, the editor I use (Edit Plus 2) will show javascript reserved words in orange so I can see them easier - onBeforeUnLoad does not display in this color leading me to wonder if it is being seen as a javascript command?
0
rfreshAuthor Commented:
I found this code on this web site (http://www.morningz.com/sections/utils/showPage.aspx?PageID=40):

While on this page the code pops up a window with OK and Cancel - if I click on OK I stay move on to another web page - if I click Cancel I stay on that web page - I tried putting the code below in my own php file but it does not popup the OK/Cancel window. I copied the code into it's own file (Test.html) and ran it - same results: no popup window - can someone see why it works in their web site but not if you try to use it by itself?

<html><head>
<script language="javascript">
g_blnCheckUnload = true;     // Initialize to check for unload
function RunOnBeforeUnload() {
   if (g_blnCheckUnload) {window.event.returnValue = '* Your text to add to the confirm box *';}
function bypassCheck() { g_blnCheckUnload  = false; }
</script>
</head>
<body onBeforeUnload="RunOnBeforeUnload();">
<br /><br /><a href="http://www.msn.com">[ To MSN ]</a> with page check
<br /><br /><a href="http://www.msn.com" onClick="bypassCheck()">[ To MSN ]</a> without check
<br /><br /></body></html>
0
superslamwichCommented:
Make it this:

<html><head>
<script language="javascript">
  g_blnCheckUnload = true;     // Initialize to check for unload
  function RunOnBeforeUnload() {
    if (g_blnCheckUnload) {
      window.event.returnValue = '* Your text to add to the confirm box *';
    }
  }
  function bypassCheck() {
    g_blnCheckUnload  = false;
  }
</script>
</head>
<body onBeforeUnload="RunOnBeforeUnload();">
<br /><br /><a href="http://www.msn.com">[ To MSN ]</a> with page check
<br /><br /><a href="http://www.msn.com" onClick="bypassCheck()">[ To MSN ]</a> without check
<br /><br /></body></html>

It didn't have the closing bracket for the RunOnBeforeUnload() function.

{Slam}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.