1. Page output
1. header file
Copy codeThe Code is as follows:
<Head>
<Script language = "javascript">
Document. write (" www.www.jb51.net ");
</Script>
</Head>
2. on the page
Copy codeThe Code is as follows:
<Body>
<Script>
Document. write (" ");
</Script>
</Body>
3. External files
<Script src = "display. js"> </script>
4. Use innerHtml of the page ID
Copy codeThe Code is as follows:
<Script>
Window. onload = writeMessage; // call the writeMessage function during page loading.
WriteMessage (){
Document. getElementById ("helloMessage"). innerHTML = "";
// Locate the dom ID (helloMessage) and modify its html content
}
</Script>
5. Warning
Alert ("Guangzhou Baihui logistics Co., Ltd ");
6. Ask
Copy codeThe Code is as follows:
If (confirm ("whether to visit our Homepage "))
{
Alert ("yes, go ");
}
Else {
Alert ("quit ");
}
7. Enter
Copy codeThe Code is as follows:
Var ans = prompt ("Enter your message", "hello ,");
If (ans ){
Alert ("you said:" + ans );
}
Else {
Alert ("exit, no message ");
}
8. page Jump
Copy codeThe Code is as follows:
<Script>
Window. onload = initAll;
Function initAll (){
Document. getElementById ("redirect"). onclick = initRedirect;
}
Function initRedirect ()
{
Window. location = "index.html ";
Return false;
}
</Script>
<A href = "http://www.jb51.net" id = "redirect"> foot home </a>
9. Determine the branch
Copy codeThe Code is as follows:
<Script>
Window. onload = initAll;
Function initAll (){
Document. getElementById ("Lincoln"). onclick = saySomething;
Document. getElementById ("Kennedy"). onclick = saySomething;
Document. getElementById ("Nixon"). onclick = saySomething;
}
Function saySomething (){
Switch (this. id ){
Case "Lincoln ":
Alert ("Four score and seven years ago ...");
Break;
Case "Kennedy ":
Alert ("Ask not what your country can do for you ...");
Break;
Case "Nixon ":
Alert ("I am not a crook! ");
Break;
Default:
}
}
</Script>
<Form action = "#">
<Input type = "button" id = "Lincoln" value = "Lincoln"/>
<Input
Type = "button" id = "Kennedy" value = "Kennedy"/>
<Input type = "button" id = "Nixon"
Value = "Nixon"/>
</Form>
10. Exception capture
Copy codeThe Code is as follows:
Window. onload = initAll;
Function initAll (){
Var ans = prompt ("input parameter :","");
Try {
If (! Ans | isNaN (ans) | ans <0 ){
Throw new Error ("input is not a number ");
}
Alert ("root" + ans + "yes" + Math. sqrt (ans ));
}
Catch (errMsg ){
Alert (errMsg. message );
}
}