Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2611
  • Last Modified:

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...
0
rfresh
Asked:
rfresh
  • 2
  • 2
1 Solution
 
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now