You are here: irt.org | FAQ | JavaScript | Redirect | Q1218 [ previous next ]
Here are two ways:
<html> <head> <script language="JavaScript" TYPE="text/javascript"><!-- function checkIt(){ if (confirm("Are you sure you want to do this?")) { return true; } else { document.thisForm.action = "pageb.htm"; return true; } } //--></script> </head> <body> <form action="pagea.htm" method="post" onSubmit="return checkIt();" name="thisForm"> <input type="text" name="name" size="20"> <input type="submit" value="Submit"> </form> </body> </html>
And the second:
<html> <head> <script language="JavaScript" TYPE="text/javascript"><!-- function checkIt(){ if (confirm("Are you sure you want to do this?")) { location.href="pagea.htm"; } else { location.href="sideB.cfm"; } } //--></script> </head> <body> <form name="thisForm"> <input type="text" name="name" size="20"> <input type="button" value="Submit" onClick="checkIt()"> </form> </body> </html>
What is the difference? There is a major difference between the two scripts. The first one changes the action of the form, depending on whether you choose "OK" or "Cancel" on the Javascript box. The second one merely uses location.href, again dependent on which button you select. The difference is that in the first script, ALL FORM FIELDS ARE SUBMITTED. The second script merely does a redirect to one page or the other, but doesn't pass any of the form fields.