Click the link below and see what happens!
If I press the OK button then I go to the index page. If I press the CANCEL button then I go to the alerts page. Here is the code in the HEAD of this page:
<script type= "text/javascript"><!-- Start hiding code form old browsers. function ConfirmChoice2(){ answer = confirm( "Do you really want to go to the contents? If you press cancel you will go to alerts!")if (answer ==1){ location = "index.html"} else if (answer==0){ location= "alerts.htm";} } End of Hiding --> </script> |
Apart from renaming the function ConfirmChoice ConfirmChoice2, the code that is different is shown in bold enlarged text. Really the only new stuff is the else statement. What follows is a repetition of the code for the first URL with the second URL. That is the LOCATION in the second option is alerts.htm instead of index.html!
The annoying thing about Java Script, or any computer language, is not writing programs, but silly things, like spelling, forgetting to end a line with a semicolon, or ending a line with a semicolon when you shouldn't, etc. In the above code, the line:
answer = confirm(
"Do you really want to go to the contents?If you press cancel you will go to alerts!
")
Is broken after the question mark. Notice that the coloring has gone wrong too, indicating an error in the code (in jsEditor and similar programs) (If you break a line of text (sometimes called a string) in Java Script you get an error. Note that the following is not broken:
answer = confirm("Do you really want to go to the contents? If you press cancel you will go to alerts!")
If you copy it by selecting it and choosing copy, then you will copy one line. It is wrapped on the page, but not broken. If you were to type the above as it appears to be (in two lines), then it would be broken.
If you write long lines of text, the other computer, operating system, browser, etc, might break it and give you an 'un-terminated string error'. It is good practice really to separate long strings to avoid this. Do it like this:
answer = confirm(
"Do you really want to go to the contents? "+"If you press cancel you will go to alerts!"
)
We can join two lines in a string by writing:
"Last bit of broken string "
+"Rest of string"
Remembering to include spaces between the start of one string and the end of the other, if necessary. And remember to use the same quote types, single or double! That is, don't start with double quotation marks and end with single ones - you'll confuse the poor computer.
In summary, when using code from this tutorial (or any tutorial, or a book), you might have problems with broken or unterminated strings if you type the code exactly as it appears. It is a good learning experience to type the code rather than copy it with the mouse, but you need to ensure that anything between quotes must be on one line!
One equals sign is good enough for me and I suppose you aren't greedy either, but computers need two in the above application! The word answer, above, is a variable which means it represents something like a word or a number. It has a value, which in computers can be a number or text. We can give some value to our variable, or we can check what value it contains. In our program, we want to know whether it is "1" or "0", that is we want to know which button the user has pressed. So we want to compare the value of answer with "1" or "0" to find which matches. To compare we use two operators such as = = to distinguish it from the normal single equals sign. To assign a value we use the normal operators(-, +, =, etc.) So if we say, as part of some code:
Then the variable answer is assigned the value of 1. If we say:answer=1
//wrong for condition statements - only one equals sign
for condition statements - two equals signsanswer==1 //right
Then we are asking the computer if the value of answer is 1. (I made if big because we always use comparison operators in if statements!)
In the above code we used:
answer = confirm(
"Do you really want to go to the contents? If you press cancel you will go to alerts!")
One equals sign because we want to give the variable answer the value of the confirm button. We use an assignment operator. In the if statements, we use two operators because we are comparing values. If you write a statement like:
if
answer=1 //wrong - only one equals
The computer will laugh its socks off, because you are asking it if something has a certain value, and using the assignment operator to tell it that it is this value.
if
answer==1 //right - two equals
We have therefore given the user the choice of two URLs for the price of clicking one!
Let's look at those infuriating Hello and Goodbye Alerts