Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript works in all but moz based browsers - could use some help

Status
Not open for further replies.

efoivx

Programmer
Joined
Aug 7, 2003
Messages
6
Location
US
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.

Code:
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');
			}
		}
}
 
Put an alert message as the first line of the function:

alert(form.changed.value);

This will let you know if the function is even being called and, if it is, what it things the form.changed.value is.

Is your form named 'form'? Maybe Mozilla has a problem with that (since form is also a tag). Just looking at possible conflicts...

Maybe Mozilla wants you to declare question as a var instead of just creating it on the fly.

I don't use Mozilla, but hopefully something here might help.

Good luck. Post back with the results of the alert message.

--Dave
 
How are you calling this? It's also a good idea NOT to name your form "form", as it can cause problems with almost any browser. If your form action doesn't have an onSubmit() statement, then you'll need to preface your references to elements with "document" as in "document.form.change.value".

There's always a better way. The fun is trying to find it!
 
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.

Code:
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
 
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...
 
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.


I appreciate all advice and help you can offer.
 
try just setting location with no top. before it...

otherwise, window.location might be better since it doesn't look like you are running any frames...
 
correct no frames are used and i have revised the code in the example too... I am simply using location = theLInk now

I will try document.location though
 
adding document.location = instead of location =
fixes it for mozilla but MSIE STILL still doesnt work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top