/*
Basic JavaScript Learning Summary
This summary uses the DW to write, in order to debug conveniently, will affect the test part to write in the annotation form ****/
All tests are shown with the alert () function
1, if conditional sentence function funif () {
var date = new Date ();//Get a new date
var time = date.gethours ();//Get the current hour from the date
if (Time < 10) {
Alert ("Good morning!" ");
}else if (Time < 13) {
Alert ("Noon!");
}else if (Time < 18) {
Alert ("The Afternoon is up!");
}else{
Alert ("Night");
}
}
2. Switch statement function Funswitch () {
var t = document.getElementById ("Switch"). Value;
Switch (t) {
Case 1:alert ("You have entered 1! "); break;
Case 2:alert ("You have entered 2! "); break;
Case 3:alert ("You have entered 3! "); break;
Case 4:alert ("You have entered 4! "); break;
Case 5:alert ("You have entered 5! "); break;
Default:alert ("Input is not 1 ~ 5!");
}
}
3, Warning box warning box is often used to ensure that users can get some information//alert ("text"); Most of the examples in the summary are alert ();
4, Confirm box confirmation box is used to enable the user to verify or accept certain information//confirm ("text");
5, Prompt box prompt box is often used to prompt the user to enter a value before entering the page//prompt ("text", "Default value");
6. Functions/Declaration syntax: function function name (argument list) {several statements}
function, you can return a value with return, or end a function with return;
7, the lifetime of the JavaScript variable//When you declare a variable within a function, you can only access the variable in that function. When you exit the function, the variable is revoked. This variable is called
Local variables. You can use local variables with the same name in different functions, because only the function that declares the variable can recognize each of these variables.
If you declare a variable outside of a function, all functions on the page can access the variable. The lifetimes of these variables begin after they are declared and end when the page closes.
8. Cyclic//a) for loop
for (variable = start value; variable <= end value; variable = variable + step value)
for (statement 1; Statement 2; statement 3) {JavaScript statement} The statement here can be omitted, but the semicolon cannot be omitted
b) while (statement 1) {JavaScript statement} Statement 1 indicates the condition of the end of the loop
c) The Do{javascript statement}while (statement 1) Statement 1 represents the condition at which the loop ends, at least once
D) for...in cycle
For ... In declaration for looping operations on an array or an object's properties
Syntax for (variable in object)
function Funforin () {
var x;
var temp = new Array ();
Temp[0] = "0"; temp[1] = "1"; temp[2] = "2"; temp[3] = "3";
for (x in temp) {
document.getElementById ("ForIn"). Innerhtml=x;
var t = setTimeout (Funforin (), 1000);
}
}
D) The break and continue statements are used in the difference between loops, which break jumps out of the loop, and continue is used to skip the current loop, starting with the next value
9. JavaScript events//Examples: mouse click, page or image loading, mouse over, check input box in form, confirm form, keyboard key
The usual event functions are:
A) onload and onUnload
The onload event is triggered when the user enters the page, and the OnUnload event is triggered when left
b) Onfocus,onblur and onchange these three events usually match the validation form
c) OnSubmit, validate all form fields when submitting the form.
d) onmouseover and onmouseout mouse pointing, mouse removal
10 exceptions//syntax examples:
try{Running code}catch{handling Error}
Instance:
<script type= "Text/javascript" >
var txt= ""
Function message ()
//{
Try
// {
Adddlert ("Welcome guest!")
// }
catch (Err)
// {
Txt= "There is an error on this page. \ n "
txt+= "Error Description:" + Err.Description + "\ n"
txt+= "Click OK to continue. \ n "
Alert (TXT)
// }
//}
</script>
<body>
<input type= "button" value= "View message" onclick= "message ()"/>
</body>
JavaScript Primer Basics Summary (2)