On leaving page

U

UKuser

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
 
J

Janwillem Borleffs

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
 
B

Bill H

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
 
J

Janwillem Borleffs

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
 
T

Thomas 'PointedEars' Lahn

Bill said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top