13-IntroJavascript
13-IntroJavascript
CS380
What is Javascript?
2
CS380
Linking to a JavaScript file:
4
script
<script src="filename" type="text/javascript"></script>
HTML
script tag should be placed in HTML
page's head
script code is stored in a separate .js file
JS code can be placed directly in the
HTML file's body or head (like CSS)
but this is bad style (should separate
content, presentation, and behavior
CS380
Event-driven programming
5
// single-line comment
/* multi-line comment */
JS
CS380
Logical operators
11
CS380
for loop (same as Java)
13
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
CS380
while loops (same as Java)
14
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
CS380
Popup boxes
15
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
CS380