Solved

With Confirm() Can I Stop Back Button Action?

Posted on 2003-10-29
5
2,604 Views
Last Modified: 2009-12-16
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
Comment
Question by:rfresh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:superslamwich
ID: 9647266
Try this:

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

{Slam}
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9647293
> 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
 

Author Comment

by:rfresh
ID: 9652005
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
 

Author Comment

by:rfresh
ID: 9652295
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
 
LVL 3

Accepted Solution

by:
superslamwich earned 50 total points
ID: 9653249
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question