Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > On leaving page

Reply
Thread Tools

On leaving page

 
 
UKuser
Guest
Posts: n/a
 
      09-07-2007
Hi folks,

Can anyone tell me why this is not working, where the script is placed
in the header section and document.pressed=this.value; is in the
onClick statement of a form submit button.

window.onbeforeunload =
function OnSubmitForm(){
if (document.pressed='Save1'){
return true;
} else {
return("You will lose any unsaved info");
}
}

I'm trying to get it so that you only get the error when you make a
move other than clicking the Continue button.

Any help would be great, as I'm not a javascript guru.

Thanks

 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      09-07-2007
UKuser schreef:
> window.onbeforeunload =
> function OnSubmitForm(){
> if (document.pressed='Save1'){
> return true;
> } else {
> return("You will lose any unsaved info");
> }
> }
>


I don't know the document.pressed property (custom one?), but if you do
it as follows, it might work:

function OnSubmitForm() {
if (document.pressed == 'Save1'){ // '=' is an assignment
return true;
} else {
return("You will lose any unsaved info");
}
}

window.onbeforeunload = OnSubmitForm;


JW
 
Reply With Quote
 
 
 
 
Bill H
Guest
Posts: n/a
 
      09-07-2007
On Sep 7, 6:50 am, UKuser <(E-Mail Removed)> wrote:
> Hi folks,
>
> Can anyone tell me why this is not working, where the script is placed
> in the header section and document.pressed=this.value; is in the
> onClick statement of a form submit button.
>
> window.onbeforeunload =
> function OnSubmitForm(){
> if (document.pressed='Save1'){
> return true;
> } else {
> return("You will lose any unsaved info");
> }
> }
>
> I'm trying to get it so that you only get the error when you make a
> move other than clicking the Continue button.
>
> Any help would be great, as I'm not a javascript guru.
>
> Thanks


I think you want to return false, not an error message. I could be
wrong, but doesnt the submit handler just look for a value of true or
false and default to true if false is not false?

Bill H

 
Reply With Quote
 
Janwillem Borleffs
Guest
Posts: n/a
 
      09-07-2007
Janwillem Borleffs schreef:
> function OnSubmitForm() {
> if (document.pressed == 'Save1'){ // '=' is an assignment
> return true;
> } else {
> return("You will lose any unsaved info");
> }
> }
>
> window.onbeforeunload = OnSubmitForm;
>


Bill H is right about his comment, so the OnSubmitForm function can be
reduced into the following:

function OnSubmitForm() {
return document.pressed == 'Save1';
}


JW


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      09-07-2007
Bill H wrote:
> On Sep 7, 6:50 am, UKuser <(E-Mail Removed)> wrote:
>> Can anyone tell me why this is not working, where the script is placed
>> in the header section and document.pressed=this.value; is in the
>> onClick statement of a form submit button.
>>
>> window.onbeforeunload =
>> function OnSubmitForm(){
>> if (document.pressed='Save1'){
>> return true;
>> } else {
>> return("You will lose any unsaved info");
>> }
>> }
>>
>> I'm trying to get it so that you only get the error when you make a
>> move other than clicking the Continue button.
>>
>> Any help would be great, as I'm not a javascript guru.

>
> I think you want to return false, not an error message.


No, that approach is correct (to a certain extent). A peculiarity of the
proprietary `onbeforeunload' event handler is that if the return value is
a string value, it is displayed in a message box asking whether the user
actually wants to navigate away. As a precaution against scriptkiddies,
that event cannot be canceled anymore (IIRC that change was made with
the Firefox 2.0 release). Returning `true' or another non-string value
displays the same as returning the empty string (at least in my Firefox
2.0.0.6 on Windows XP SP2).

> I could be wrong, but doesnt the submit handler just look for
> a value of true or false and default to true if false is not false?


There is no event handler for the `submit' event here.

Different event handlers require different return values. However, either
`true' or `false' are usually the values required to return in order to
prevent the default action and further bubbling (to cancel the event) this
way, depending on the event handler. For the `submit' event, it may be
possible only to return `false' to cancel the event and return another
true-value otherwise; however, I would always make that little effort and
return `true' in the latter case instead just to be on the safe side.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warn user before leaving page with unsaved changes markalroberts@gmail.com ASP .Net 7 02-28-2011 08:37 PM
Capturing the leaving of a page/http_request =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?= ASP .Net 1 11-17-2005 02:05 PM
Confirming leaving the page..... UJ ASP .Net 4 08-10-2005 06:17 PM
Leaving a Page brian ASP .Net 4 06-12-2004 01:12 AM
Create cookie in what event when leaving page? Rich ASP .Net 1 02-20-2004 11:13 PM



Advertisments