What is an entity? What are some common HTML entities that you know? Answer: An HTML entity
is a symbol that cannot be directly represented by a keyboard character.
It is of the form &entity_name; Here are some common
entities:
Entity Symbol
< <
> >
& &
" "
' '
nonbreaking space
β β (Greek letter beta)
→ → (right arrow)
“ “ (left double quotes)
Why are the outputs on the same line?
I thought that writeln puts a newline
character (\n) at the end of its output. Place these statements
in this test script before viewing the HTML page:
<html>
<body>
<script>
// Copy JavaScript code to test here.
</script>
</body>
</html>
Answer: Just because the HTML code goes to a new line doesn't mean
that the browser will display it on a new line. The \n character counts
as whitespace that is eliminated by the browser.
You need <br> to force
the browser display to go to a new line:
Add a link to page1.html with target page2.html.
Show Page 2 in a new window.
Add a link to page2.htm with target page1.htm.
Show Page 1 in a new window.
Ans: Here is a zipfile of these folders and files:
foldera.zip.
You upload your Project 0 to the studentweb server and
try to view it in a browser using this URL:
http://studentweb.cdm.depaul.edu/ssmith/index.htm
However, you obtain this message:
Not Found
The requested URL /~ssmith/index.htm was not found on this server.
What might be wrong?
Answer: You might not have uploaded your files to the public_html folder. Perhaps you
misspelled a folder or file name, for example, maybe index.htm is spelled as
index.html
Why is the label control needed? Why not just use HTML text?
Answer: The label control has two effects: (1) when the label of a textfield
is clicked, the focus goes to the corresponding textfield. Having the
focus means that that when the user types at the keyboard, the typed text
is entered in the textfield with the focus.
(2) When a screen reader is running and a textfield is clicked or arrived at via
tabbing, the contents of the textfield and the label of the textfield are read.
What is the difference between these two HTML tags?
You can't put HTML markup in the value attribute of the <input> tag.
Write a web page that looks like this:
The button should display the text "I've been clicked." when the button is clicked.
Give three solutions: (a) using an onclick attribute in the
<button> tag,
(b) using the addEventListener method in an init method,
(c) and setting the onclick
Answer: here is the source code,
including HTML code and JavaScript code. using the
property of the button object that represents the button tag.
What is JavaScript?
In September 1995 JavaScript was created in 10 days by Brendan Eich of
Netscape Communications.
It is a lightweight scripting language, used to build websites -- about 1.52
billion of them, 95% of all websites.
Originally, the name of JavaScript was Mocha. It was designed as a web
scripting companion to Java.
The name Mocha was changed to LiveScript for marketing purposes.
In 1998, AOL acquired Netscape and formed an alliance with Sun Microsystems
to develop web software. At that time that the name of LiveScript was
changed to JavaScript. This was a marketing ploy, based on the popularity of Java.
JavaScript and Java are very different, but the two languages have many
syntax elements in common.
ECMAScript (starting in 1997) is a standard for JavaScript, which is meant
to ensure the interoperability of web pages across different browsers. ECMA
means European Computer Manufacturers Association.
Using the Chrome Browser, click on the three dots icon in the upper right
corner; Select More tools >> Developer tools. In the HTML code
for Item 1, replace the line
document.writeln("Hello, World.");
with
console.log("Hello, World.");
Refresh the browser.
Writing to the browser console is useful for debugging.
Display a popup window
In the HTML code for Item 1, use
alert("Hello, World.");
instead of
console.log("Hello, World.");
Display Output in a Textfield
You already know how to do this from IT 130. We will look
at other possibilities this later.
Practice Quiz
Take PracticeQuiz2a.
Test Script
To test the JavaScript code for the rest of these notes, use this test script:
<!DOCTYPE html>
<html>
<body>
<script>
// Copy JavaScript code to test here.
</script>
</body>
</html>
JavaScript Datatypes
number -- JavaScript numbers are represented as 64 bit
(double precision) floating point numbers:
35342, -8976, 0, 34.3827, -1.359, 4.565e16, -9.37e-31
Try out this web page:
<!DOCTYPE html>
<!-- MaxValue Example -->
<html>
<body>
<script>
var n = 2;
// Repeat for loop 11 times.
for(var i = 1; i <= 11; i++) {
document.write(n + " ");
n = n * n;
}
document.writeln("<br><br>");
document.writeln(
"<br>Maximum Number Value:<br>" +
Number.MAX_VALUE + "<br>");
</script>
</body>
</html>
// Output:
2 4 16 256 65536 4294967296 18446744073709552000
3.402823669209385e+38 1.157920892373162e+77
1.3407807929942597e+154 Infinity
Maximum Number Value: 1.7976931348623157e+308
The special values Infinity and NaN are also values of the Number datatype.
Infinity is a numeric value that is larger that the largest legal
Number value, which is
1.7976931348623157e+308. The value NaN
is obtained from a numeric computation that does not produce a legal floating
point value.
string -- A string of characters delimited by
single or double quotes: 'dog', "horse", 'supercalifragilisticexpialidocious',
"243567", "!@#$%^&*()",
' ' (space), "" (null string)
boolean -- true or
false
undefined -- Special value assigned to a
variable that is
declared, but not yet assigned a value.
null -- Value assigned by the programmer to a
variable to show
that the variable has no value. Try out this statements in a script:
// Undefined Example
var s;
document.writeln(s + "<br>");
var t = null;
document.writeln(t + "<br>");
// Output:
undefined
null
Two additional Java datatypes that we will not discuss
are bigint and symbol.
A function is a set of statements designed to perform a task.
A function must be called or invoked to execute the statements.
Test this function that converts a length from inches
to centimeters:
// BasicFunction Example
// Function definition:
function inToCm(inches) {
cm = inches * 2.54;
return cm;
}
// Function test:
var inches = prompt("Enter length in inches", "0");
var cm = inToCm(inches);
document.writeln("Length in cm: " + cm);
// If input is 2, the output is 5.08
Some Math Methods
The Math class contains methods that perform math calculations.
A function is standalone. It is not called from a class or an object.
For example, parseFloat is a function.
parseFloat("2.34") returns the numeric value
2.34.
Math.floor is a method called from the
Math class. It converts a floating point number into the next lower integer.
For example, here are the some Math.floor method calls
and return values:
Method Call
Return Value
Math.floor(2.38)
2
Math.floor(0.998)
0
Math.floor(-6.83)
-7
The Math.ceil (ceiling) method converts a floating point number to the next higher integer:
Input courseScore from a prompt box. Write
if..else statements that write Pass to the
document if courseScore >= 69.5, else write
Fail to the
document.
Write a while loop that prints all 99 verses of the
song 99 Bottles of Beer on the Wall.
Modify the while loop in Problem 2 to be a for loop.