L04 Javascript1
L04 Javascript1
<script>
function sayHi(){
alert("Hi");
}
</script>
Where to include JavaScript
Common practice:
● In the head
● At the end of body
<script>
function sayHi(){
alert("Hi");
}
</script>
Where to include JavaScript
In the head
<head>
<title>JavaScript Example</title>
<script>
function sayHi(){
alert("Hi");
}
</script>
</head>
Where to include JavaScript
...
<script>
function sayHi(){
alert("Hi");
}
</script>
</body>
</html>
External JavaScript
<script>
function sayHi(){
alert("Hi");
}
</script>
<script>
function changeToFrench(){
document.getElementById("french").innerHTML = "Salut!";
}
</script>
Change style by JavaScript
<script>
function changeHelloWorldStyle(){
var e = document.getElementById("hello");
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
</script>
Basic JavaScript syntax
function changeHelloWorldStyle(){
var e = document.getElementById("hello");
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
Basic JavaScript syntax
JavaScript Comments
Code after double slashes // or between /* and */ is treated as a comment.
Comments are ignored, and will not be executed.
/*
this function change the style of the text
*/
function changeHelloWorldStyle(){
//get the html element
var e = document.getElementById("hello");
//change the style
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
Basic JavaScript syntax
JavaScript uses the var keyword to declare variables.
underscore:
student_name, student_id, first_name, last_name
camel case:
studentName, studentId, firstName, lastName
Basic JavaScript syntax
var x; // x is undefined
alert(x);
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
Basic JavaScript syntax
Assignment operators
var x;
var x;
var x = 5;
var positive = (x > 0);
if(positive){
alert("x is positive");
}
Basic JavaScript syntax
Comparison and Logical Operators
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
Basic JavaScript syntax
== equal to
=== equal value and equal type
var x = 5;
var y = "5";
if(x == y){
alert("yes");
}else{
alert("no");
}
Basic JavaScript syntax
== equal to
=== equal value and equal type
var x = 5;
var y = "5";
!= not equal
!== not equal value or not equal type
var x = 5;
var y = "5";
if(x != y){
alert("yes");
}else{
alert("no");
}
Basic JavaScript syntax
!= not equal
!== not equal value or not equal type
var x = 5;
var y = "5";
var x = 5;
var y = "x is " + ( x%2==0 ? "even" : "odd" );
alert(y);
Date
alert(now);
alert(typeof now); //object
Date
//using YYYY-MM-DDTHH:MI:SS
var d = new Date("2000-01-30T10:00:00");
alert(d);
Date
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
alert(subjects); //ISIT206,MATH121,CSCI301
!!!
var arrayName = [item0, item1, ...];
alert(subjects[0]); //ISIT206
alert(subjects[1]); //LOGIC101
alert(subjects[2]); //CSCI301
alert(subjects[3]); //LAW201
Array
Length of array
subjects[1] = "LOGIC101";
subjects[3] = "LAW201";
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
}; //it is better to write this way
alert(info.name); //John
alert(info["name"]); //John
obj.property
obj["property"]
Object
Change the values of an object
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
};
// two ways:
info.year = 1;
info["year"] = 1;
Object
Delete object properties
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
};
// two ways:
delete info.year;
delete info["year"];
Object
Create an empty object
var info = { };
info.firstName = "John";
info.lastName = "Lee";
alert(info["firstName"]);
alert(info.lastName);
Array vs Object
arrayName[0] = "LOGIC101";
arrayName[1] = "CSCI111";
objectName["firstName"] = "John";
objectName.lastName = "Lee";
Array Sorting
var subjects = ["ISIT206", "MATH121", "CSCI301"];
subjects.sort();
In general:
the_array_to_be_sorted.sort(the_sorting_function ...);
http://www.uow.edu.au/~dong/w3/example/js/ninja.html
Array Sorting Before sorting
ninja_results = [
ninja_results.sort(
{name: "John", level: 4, seconds: 85},
function (p1, p2) {
{name: "Peter", level: 2, seconds: 35},
if (p1["level"] > p2["level"]){
{name: "Kate", level: 4, seconds: 80},
return 1; // sort
{name: "Luke", level: 5, seconds: 120}
}
];
if (p1["level"] < p2["level"]){
return -1; // don’t sort
}
//at this point the two persons have the same level
if (p1["seconds"] < p2["seconds"]){
return 1; // sort
}
if (p1["seconds"] > p2["seconds"]){
return -1; // don’t sort After sorting
} ninja_results = [
When a confirm box pops up, the user will have to click either "OK" or "Cancel".
If the user clicks "OK" the box returns the input value.
prompt("sometext","defaultText");
http://www.w3schools.com/js