JCL's Tutorial On JavaScript Confirms

Note : If a dialogue box did not pop up, you are not using a high enough browser. You need Netscape or Internet Explorer 2.0.2 or higher.
Also Note : If you want this confirm on your page, but do not want to read this tutorial, click here.

Creating JavaScript Confirms

JavaScript Confirms can be used for practically anything (ex., asking your visitor if they want to close their browser window, asking if he/she wants to stay on the page, etc.). They are pretty easy to make and they can be very useful. Place this script in your HTML document under the <html> tag. Below is the document source I created.


<script language = "JavaScript">
<!-- Begin hiding here --

/********************************************************************************************
This script is created and ©1997 by Jason Schanker (jcheetah@orion.webspan.net).
This script comes from "http://www.webspan.net/~herbs/jcheetah/web_design/javascript/confirm".
You may copy this source freely if these comments remain in the script.
********************************************************************************************/

if(!confirm("Hello And Welcome to JCL's Tutorial On JavaScript Confirms. You Can Make This Confirm Say Whatever You Want And Do Whatever You Want. Press OK or Yes To Proceed, Or Press No Or Cancel To Go Back To The Site You Were On Before.")) history.go(-1);

// -- End hiding here -->
</script>

What the Code Means

<script language = "JavaScript">

All this does is tell the computer the language that is going to be used. You must use this tag, <script>, or <script = "JavaScript"> to make this Confirm or any other JavaScript work.

<!-- Begin hiding here --

Since some browsers can't read JavaScript, they just read over the JavaScript code as text and show it to the user as text. This fragment of code begins the commenting out of this text if the user's browser cannot read JavaScript.

/********************************************************************************************
This script is created and ©1997 by Jason Schanker (jcheetah@orion.webspan.net).
This script comes from "http://www.webspan.net/~herbs/jcheetah/web_design/javascript/confirm".
You may copy this source freely if these comments remain in the script.
********************************************************************************************/

These are a bunch of comments which must be left in here if you use this script. The /*,anything after it and before */ are comments. When the computer gets to this part, it skips over it.

if(!confirm("Hello And Welcome to JCL's Tutorial On JavaScript Confirms. You Can Make This Confirm Say Whatever You Want And Do Whatever You Want. Press OK or Yes To Proceed, Or Press No Or Cancel To Go Back To The Site You Were On Before.")) history.go(-1);

This command is an if statement and a confirm. To explain how it works in an easy fashion, I will explain the command in steps.

1. The computer spots an if statement and tries to find out if it is true or not.
2. The computer sees a confirm and activates it to see the truth value of the function, confirm() (The confirm() function is a predefined function (a function that's already been compiled and defined).
3. If the user presses No or Cancel, the confirm returns false so therefore the if statement is true since ! means not or nopt true. If the if statement is true, the visitor will be taken to the page before because the history.go() function tells the computer to go to the site you visited n pages before or after where n is the integer inside of the function's parentheses. If you have -n in the parentheses, your visitor will go to the site he/she was on n pages ago. If n is a positive number, your visitor will go to the site he/she was on n pages after your site. Since n is - 1 in the code I created, if you clicked No or Cancel when the Confirm popped up, you would go to the page you were on before coming to this page. If you change the -1 to 1, it will go forward a page. If the user didn't go to a page before this one and pressed No or Cancel when the confirm popped up, nothing will happen. If the user pressed Yes or OK, the if statement is false so the computer will skip over it and will stay on the page.

By the way, I didn't make a mistake when I used )) in the last line. If you look at the beginning of the command, you will see a parenthesis after the if and one after the confirm.

// -- End hiding here -->

This ends the commenting out for browsers unable to read JavaScript.

</script>

This command tells the computer that the end of the JavaScript source has been reached. If you do not include this in your document source, the rest of your HTML document will be considered as JavaScript and the computer will not be able to comprehend the rest of the document. Also, the computer will give you Error Alerts.

JCL's HTML & JavaScript Tutorial
E-Mail Me