- Scope and Visibility
- switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is
different from case 1 and 2
}
- <html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head><body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" >
</form>
</body>
</html>
- <html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
- <html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>
- var x=5;
var x;
after these statements, x will still have value 5. not a useful part of the language.
- var x = 5;
x === 5; // true
x === "5"; // false
- When you declare a variable within a function, the variable can only be accessed within that function.When you exit the function, the variable is destroyed.
These variables are called local variables.
You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.
If you declare a variable outside a function, all the functions on your page can access it.
The lifetime of these variables starts when they are declared, and ends when the page is closed.
-
How to put into HTML page
-
use script tag
-
use comments for dumb browsers
- // is the javascript comment, to prevent it from executing the closing html comment
-
scripts in <head> section
- executed when called
- ensures that it's loaded before it might be needed
-
scripts in <body> section
- executed while the page loads
-
external scripts
- <script type="text/javascript" src="xxx.js"></script>
-
Language
-
Statements
- case sensitive
- separated by semi-colon or blank line
- can be grouped into blocks with { }
-
Comments
- Single line //
- Multi-Line with /* */
-
Variables
- must begin with a letter, or underscore
-
declaration
-
examples
- var x;
- var longNamedVariable;
- var x = 5;
- var longNamed = "Literal in quotes";
-
notes
- variables can be redclared without losing their value
-
Operators
-
Assignment
- =
- +=
- -=
- /=
- *=
- %=
-
Arithmetic
- +
- -
- /
- *
-
%
- modulus
- x=5%2; // means x = 1
- ++
- --
-
String concatenation
-
+
- if you add a number and a string, result is string
-
Comparison
- ==
-
===
- equal, value and type
- !=
- >
- <
- >=
- <=
-
Logical
- &&
- ||
- !
-
Conditional
- the shortcut if
-
variablename=(condition)?value1:value2
- eg
- x=(4<5)?"yes":"no"; // x will be yes
-
Conditional
-
if
- if (condition) statement or block
- if (condition) {} else {}
- if (condition) {} else if (condition2) {} else {}
- switch
-
Functions
- put them in the head
- not executed until called (naturally)
- example call
- procedures can be created by leaving out the return
-
Popup Boxes
-
alert("sometext");
- user must press OK
-
confirm("sometext");
- user is offered OK or Cancel. returns true or false respectively
-
prompt("sometext", "defaultValue");
- user is offered OK or Cancel. returns value or null respectively