Solved

With Confirm() Can I Stop Back Button Action?

Posted on 2003-10-29
5
2,595 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
Comment Utility
Try this:

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

{Slam}
0
 
LVL 11

Expert Comment

by:Zontar
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

762 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

11 Experts available now in Live!

Get 1:1 Help Now