Lesson 01
 
Lesson 1 - Reading

REQUIRED:

  • Read and do the practice work in Chapter 1
  • Read and do the practice work in Chapter 2

 

Lesson 1 - Notes and Practice Work

How to write text on a page.

<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
Write text with formatting
How to format the text on your page with HTML tags.
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
</html>


How to Put a JavaScript Into an HTML Page

<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>

The code above will produce this output on an HTML page:

Hello World!

Ending Statements With a Semicolon?

With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.
Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.

How to Handle Older Browsers

Browsers that do not support scripts will display the script as page content. To prevent them from doing this, we may use the HTML comment tag:
<script type="text/javascript">
<!--
some statements
//-->
</script>
The two forward slashes at the end of comment line (//) are a JavaScript comment symbol. This prevents the JavaScript compiler from compiling the line.
Note: You cannot put // in front of the first comment line (like //<!--), because older browsers will display it. Strange? Yes! But that's the way it is.

Scripts in the body section will be executed WHILE the page loads.
Scripts in the head section will be executed when CALLED.

The alert, confirm, and prompt boxes

The three "commands" involved in creating alert, confirm, and prompt boxes are:

  • window.alert()
  • window.confirm()
  • window.prompt()

Lets look at them in detail:

The first one is:

window.alert()

This command pops up a message box displaying whatever you put in it. For example:

<body>
<script>
window.alert("My name is Bob. Welcome!")
</script>
</body>

As you can see, whatever you put inside the quotation marks, it will display it.

The second one is:

window.confirm()

Confirm is used to confirm a user about certain action, and decide between two choices depending on what the user chooses.

<script>
var x=window.confirm("Are you sure you are ok?")
if (x)
window.alert("Good!")
else
window.alert("Too bad")
</script>

First of all, "var x=" is a variable declaration; it declares a variable ("x" in this case) that will store the result of the confirm box. All variables are created this way. x will get the result, namely, "true" or "false". Then we use a "if else" statement to give the script the ability to choose between two paths, depending on this result. If the result is true (the user clicked "ok"), "good" is alerted. If the result is false (the user clicked "cancel"), "Too bad" is alerted instead. (For all those interested, variable x is called a Boolean variable, since it can only contain either a value of "true" or "false").

The third one is:

window.prompt()

Prompt is used to allow a user to enter something, and do something with that info:

<script>
var y=window.prompt("please enter your name")
window.alert(y)
</script>

The concept here is very similar. Store whatever the user typed in the prompt box using y. Then display it.

 

Lesson 1 - Assignment
  1. Create an HTML file that uses an example of
    • document.write
    • alert()
    • browser detection
  2. Save the file as CIS166AA01.HTM

Send in Your Assignment to be Graded

After you have read these instructions, click on the link "Submit my assignment for grading" below. This will create a message for you to use to send in your assignment to the CIS166AA grader. It already will have the message address and the subject line filled in. DO NOT CHANGE THE ADDRESS OR THE SUBJECT LINE. Enter the following required information as the message text:

  • your full name
  • your palette account username
    • If you do not know your palette username, use your student ID card at a palette account lookup station in HTC1 or HTC2. If you are off campus, call the Student Help Desk at 623.845.HELP (4357) or send an email message to student-helpdesk@student.gc.maricopa.edu to request your palette username.
  • you email address
  • course section number
  • Attach the CIS166AA01.HTM file to the email.

Only by following this process can you be guaranteed that your assignment will get graded and the score recorded correctly.

Submit my assignment for grading

 


  Back to Top