Professional Documents
Culture Documents
Recap
Programming Languages - set of instructions/code
which tells a computer what it needs to do
To make software or drivers
Scripting Languages -to perform some certain task.
used to create a website.
Markup Languages - prepare the structure/design
of a page
used to design a website
2
Client Side Scripting
3
CS380
Why use client-side programming?
4
CS380
What is Javascript?
6
CS380
A Simple Script
<html>
<head><title>First JavaScript Page</title></head>
<body>
<h1>First JavaScript Page</h1>
<script type="text/javascript">
document.write("<hr>");
document.write("Hello World Wide Web");
document.write("<hr>");
</script>
</body>
</html>
Linking to a JavaScript file:
12
script
<script src="filename" type="text/javascript"></script>
HTML
<head>
<script language=“JavaScript”>
var x = “hello”, y;
alert(“Variable x value is “ + typeof(x));
alert(“Variable y value is “ + typeof(y));
alert(“Variable x value is “ + typeof(z));
</script>
</head>
An unary operator that tells the type of its
operand.
Returnsa string which can be "number", "string",
"boolean", "object", "function", "undefined", and
"null"
Number type
16
integers and real numbers are the same type (no int
vs. double)
same operators: + - * / % ++ -- = += -= *= /=
%=
similar precedence to Java
many operators auto-convert types: "2" * 3 is 6
CS380
Comments (same as Java)
17
// single-line comment
/* multi-line comment */
JS
Java/JS/PHP: // comment
PHP: # comment
CS380
Special values: null and undefined
18
CS380
Null & Undefined example
19 <html>
<head>
<title> Null and Undefined example </title>
<script language=“JavaScript”>
var test1, test2 = null;
alert(“No value assigned to the variable” + test1);
alert(“A null value was assigned” + test2);
</script>
</head>
<body> … </body>
</html>
Logical operators
20
"5.0" == 5 is true
CS380
Type Conversion
Converting a value to a number
var numberVar = someVariable – 0;
<head>
<script language=“JavaScript”>
var result;
result = (true*10 + false*7);
alert(“true*10 + false*7 = “, result);
</script>
</head>
if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
JS
identical structure to Java's if/else statement
JavaScript allows almost anything as a condition
for loop (same as Java)
26
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
} JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo" JS
while loops (same as Java)
27
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
alert(), confirm(), and prompt()
<script type="text/javascript">
alert("This is an Alert method");
confirm("Are you OK?");
prompt("What is your name?");
prompt("How old are you?","20");
</script>
Arrays
30
function name() {
statement ;
statement ;
...
statement ;
} JS
function myFunction() {
alert("Hello!");
alert("How are you?");
} JS
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
} JS
Changing element style:
element.style
45
function changeText() {
//grab or initialize text here
<script>
document.getElementById("firstp").style.color = "#9999FF";
document.getElementById("firstp").style.fontSize = "120%";
document.getElementById("firstp").style.backgroundColor="#332277";
document.getElementById("firstp").style.textAlign = "right";
document.getElementById("firstp").style.padding = "30px";
document.getElementById("firstp").style.borderWidth = "8px";
document.getElementById("firstp").style.borderColor = "green";
document.getElementById("firstp").style.borderStyle = "inset";
</script>
</body></html>
innerHTML
The innerHTML is what is between the opening and the closing tag,
regardless of what the tag is:
<p id = “firstp”> This is the innerHTML text because it goes between the
opening and closing tag
</p>
Above, the innerHTML is: “This is the innerHTML text because it goes between
the opening and closing tag”
To change it:
document.getElementById(“firstp”).innerHTML = “new text for paragraph”
<h1 id = “firsth”>Title goes here </h1>
Above: innerHTML is: Title goes here
To change:
document.getElementById(“firsth”).innerHTML = “New Title”
innerHTML (cont.)
<p id = “linked”> <a href = “udel.edu” id = “firstlink”> link to udel </a>
</p>
What is the innerHTML of linked?
What is the innerHTML of firstlink?
How would you change the innerHTML of linked to a new link?
<ol id = “list1”>
<li id = “firstItem”> cats </li>
<li id=“seconditem”> dogs </li>
</ol>
<script>
var myArray = new Array()
myArray[0] = "hey"
myArray[1] = "hi"
myArray[2] = "wassup"
myArray[3] = "greetings"
myArray[4] = "howdie“
var num = Math.floor(Math.random() * 5)
document.write("<p> " + myArray[num] + "</p>")
</script>
What does this do?