Connecting Tech Pros Worldwide Help | Site Map

Warn to Save before leaving a page...

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 11:03 AM
Jamie Jackson
Guest
 
Posts: n/a
Default Warn to Save before leaving a page...

I've got a form that's nested in a larger application. I need to
program it so that the user is warned to save upon clicking on any
exit point in the application.

So, if they click on any of the nav buttons in the header/nav bar,
they need to get an alert:

"Would you like to continue without saving your changes?"
_Yes_ _No_

It would be a big pain to retrofit the application's nav
buttons/framework for just these forms, so I'm looking for a solution
that doesn't require editing the source code of the application's
existing navigation.

What are my options?

BTW, this application is for IE 5+ only, FWIW.

Thanks,
Jamie



  #2  
Old July 20th, 2005, 11:03 AM
bengee
Guest
 
Posts: n/a
Default Re: Warn to Save before leaving a page...

Jamie Jackson wrote:[color=blue]
> So, if they click on any of the nav buttons in the header/nav bar,
> they need to get an alert:
>
> "Would you like to continue without saving your changes?"
> _Yes_ _No_[/color]

<body onunload="if (confirm('Save changes?')) { doSomethingHere(); }">

  #3  
Old July 20th, 2005, 11:03 AM
Jamie Jackson
Guest
 
Posts: n/a
Default Re: Warn to Save before leaving a page...

On Fri, 10 Oct 2003 00:31:24 +0100, bengee
<postmaster@localhost.localdomain> wrote:
[color=blue]
>Jamie Jackson wrote:[color=green]
>> So, if they click on any of the nav buttons in the header/nav bar,
>> they need to get an alert:
>>
>> "Would you like to continue without saving your changes?"
>> _Yes_ _No_[/color]
>
><body onunload="if (confirm('Save changes?')) { doSomethingHere(); }">[/color]

Great! It still requires me to modify the application framework, but
at least it's only in one place (good news). :)

Thanks very much,
Jamie

  #4  
Old July 20th, 2005, 11:03 AM
Jamie Jackson
Guest
 
Posts: n/a
Default Re: Warn to Save before leaving a page...

On Fri, 10 Oct 2003 09:53:11 -0400, Jamie Jackson
<wasteNOSPAMbasket@bigfoot.com> wrote:
[color=blue]
>On Fri, 10 Oct 2003 00:31:24 +0100, bengee
><postmaster@localhost.localdomain> wrote:
>[color=green]
>>Jamie Jackson wrote:[color=darkred]
>>> So, if they click on any of the nav buttons in the header/nav bar,
>>> they need to get an alert:
>>>
>>> "Would you like to continue without saving your changes?"
>>> _Yes_ _No_[/color]
>>
>><body onunload="if (confirm('Save changes?')) { doSomethingHere(); }">[/color]
>
>Great! It still requires me to modify the application framework, but
>at least it's only in one place (good news). :)[/color]

I think this isn't going to work. AFAIK, by the time you get the
confirmation, you've already unloaded the page, and are halfway to the
next. Is there a way to prevent the unLoad from continuing, when the
user confirms in the negative?

I'm racking my brain trying to figure out how to do this without
retrofitting all of the nav links.

I think I may be on to something with the following, would someone
please let me know if I'm going in the right direction, or if there's
an easier way, or if there's a solution to the onUnload issue above?

function changeLinks(d) {
// this picks up all the links but those in my popup nav :-( ...it's
a start, though
for (var i=0; i < d.links.length; i++) {
// maybe save each link's old location into an array here, for
later use

// now, rewrite each link to a confirm. later, code the following
to load a new window with the old url?
d.links[i].href = "javascript: confirm('Exit before saving?')";
}
}
changeLinks(document);

Thanks,
Jamie
  #5  
Old July 20th, 2005, 11:03 AM
bengee
Guest
 
Posts: n/a
Default Re: Warn to Save before leaving a page...

Jamie Jackson wrote:[color=blue]
> I think this isn't going to work. AFAIK, by the time you get the
> confirmation, you've already unloaded the page, and are halfway to the
> next. Is there a way to prevent the unLoad from continuing, when the
> user confirms in the negative?[/color]

I think you're right there... the page is already on it's way to
"unloading" and i don't think that can be stopped.
[color=blue]
> I'm racking my brain trying to figure out how to do this without
> retrofitting all of the nav links.
>
> I think I may be on to something with the following, would someone
> please let me know if I'm going in the right direction, or if there's
> an easier way, or if there's a solution to the onUnload issue above?
>
> function changeLinks(d) {
> // this picks up all the links but those in my popup nav :-( ...it's
> a start, though
> for (var i=0; i < d.links.length; i++) {
> // maybe save each link's old location into an array here, for
> later use
>
> // now, rewrite each link to a confirm. later, code the following
> to load a new window with the old url?
> d.links[i].href = "javascript: confirm('Exit before saving?')";
> }
> }
> changeLinks(document);[/color]

This looks like a good way of doing it. Change the href's to something
like :-

d.links[i].href = "javascript: if (confirm('Exit?')) {
document.location.href = 'somefile.htm'; } else { void(0); }";

Hope this works!!

Ben

  #6  
Old July 20th, 2005, 11:04 AM
Jamie Jackson
Guest
 
Posts: n/a
Default Re: Warn to Save before leaving a page...

Thanks again Ben,

Here's what I came up with:

<script language="JavaScript1.2">
function promptBeforeExiting (oldLink) {
if (confirm("Exit without saving?")) {
window.location = oldLink;
}
}

function switchLinks(d) {
var oldLink="";
for (var i=0; i < d.links.length; i++) {
oldLink = d.links[i].href;
d.links[i].href = "javascript: promptBeforeExiting('"+ oldLink + "')";
}
}
switchLinks(document);
</script>

It's working very well for <a href>s, and now I've got to figure out
how to disable my popup links as well...

Thanks,
Jamie
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.