Solved

With Confirm() Can I Stop Back Button Action?

Posted on 2003-10-29
5
2,598 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now