Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1218 Using a confirmation box, how can I load two different pages depending on what the user chooses?

irt.org | Knowledge Base | JavaScript | Redirect | Q1218 [ previous next ]

Q1218 Using a confirmation box, how can I load two different pages depending on what the user chooses?

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.


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.