Click to See Complete Forum and Search --> : javascript form alert works in all but moz based browsers need some help


efoivx
08-07-2003, 05:00 AM
The following code is used to check if changes to a form have been made and pops an alert if the person tries to switch pages before submitting the form.

The way it works is on text change of the text fields I set a hidden field to 1 indicating a change has been made to the form.

When they click a link to a different page it runs the following script to check if the hidden field is 1 or 0 if it's 0 it simply send them to the page they requested... if it is 1 then changes have been made to the form and displays the confirm alert... and asks if they want to save the changes. if they click OK it saves the changes and sends them to the changes success page if they click cancel it simple sends them on to the requested page.

This works fine in everything but moz based browsers (netscape, moz, etc).

in moz based browsers it seems to do nothing at all.


Could anyone explain why and maybe offer a solution. I am really frustrated and could use a hand.

function doAlert(theLink) { //v1.0
if (form.changed.value=="0"){
top.location = (theLink);
}
else if (form.changed.value=="1"){
form.changed.value="0";
question=confirm("You have made changes to this page but have not saved the changes by submitting the form.\n\nIF YOU WANT TO SAVE YOUR CHANGES\n > CLICK OK.\n\nIF YOU DON'T WANT TO SAVE YOUR CHANGES\n > CLICK CANCEL");
if (question =="0"){
top.location = (theLink);
}
else{
document.form.submit();
top.location = ('update.php');
}
}
}

requestcode
08-07-2003, 06:27 AM
With Netscape and MOZ browsers when you reference form elements you have to use the following format:
document.form_name.field_name.

Try changing your script to this and see if it works:
function doAlert(theLink) { //v1.0
if (document.form.changed.value=="0"){
top.location = (theLink);
}
else if (document.form.changed.value=="1"){
document.form.changed.value="0";
if (confirm("You have made changes to this page but have not saved the changes by submitting the form.\n\nIF YOU WANT TO SAVE YOUR CHANGES\n > CLICK OK.\n\nIF YOU DON'T WANT TO SAVE YOUR CHANGES\n > CLICK CANCEL");){
document.form.submit();
top.location = ('update.php');
}
else{
top.location = (theLink);
}
}
}

Also not sure if "form" is the name of your form, but if it is you should not use that. It could cause problems. If it is the only form on your page then you could use "forms[0]" that refers to the first form. You could also give it a name like "myform" and such.

efoivx
08-07-2003, 12:04 PM
I got it working -

seems moz based javascript doesn't like form to be called form... so forms must have a name.

here is the fixed code i am now using in case anyone cares to know.


function doAlert(theLink) { //v1.0
var f = document.editorform ;
if (f.changed.value=="0"){
top.location = theLink;
}
else if (f.changed.value=="1"){
f.changed.value="0";
if (confirm("You have made changes to this page but have not saved the changes by submitting the form.\n\nIF YOU WANT TO SAVE YOUR CHANGES\n > CLICK OK.\n\nIF YOU DON'T WANT TO SAVE YOUR CHANGES\n > CLICK CANCEL")){

f.submit();
//top.location = 'update.php';
}
else{
top.location = theLink;
}
}
}




thanks for the help I appreciate it very much

pyro
08-07-2003, 12:28 PM
Originally posted by efoivx
seems moz based javascript doesn't like form to be called form... so forms must have a name.Or, you could use the forms array, and refference it like this:

document.forms[0]

efoivx
08-07-2003, 05:33 PM
well it works in everything but Internet Explorer now :-\ I wasn't testing IE cause it had worked with the old code and figured this was improved code.

So I suppose I will have to do a test for browser and then run the code that works for that browser...

efoivx
08-08-2003, 05:12 PM
A simply test version is online at the following address with more information...
all the code is contained in the pages if you want to view source or download the files... the php page is not really a php page it's just for testing and servers no functional purpose but as a reference for the form action.

http://dozx.com/test/test1.html

I appreciate all advice and help you can offer.