JS usage and function summary page 1/4
Last Update:2018-12-08
Source: Internet
Author: User
1. Create a script block
1: <script language = "JavaScript">
2: JavaScript code goes here
3: </SCRIPT>
2. Hide scriptsCode
1: <script language = "JavaScript">
2: <! --
3: Document. Write ("hello ");
4: // -->
5: </SCRIPT>
Related code will not be executed in browsers that do not support JavaScript.
3 display when the browser does not support 1: <NoScript>
2: Hello to the non-JavaScript browser.
3: </NoScript>
4. Link an external script file
1: <script language = "JavaScript" src = "/" filename. js "> </SCRIPT>
5. Comment the script
1: // This is a comment
2: Document. Write ("hello"); // This is a comment
3 :/*
4: all of this
5: is a comment
6 :*/
6. output to the browser
1: Document. Write ("<strong> Hello </strong> ");
7. Define Variables
1: var myvariable = "some value ";
8 string Addition
1: var mystring = "string1" + "string2 ";
9 string SEARCH 1: <script language = "JavaScript">
2: <! --
3: var myvariable = "Hello there ";
4: var thereplace = myvariable. Search ("there ");
5: Document. Write (thereplace );
6: // -->
7: </SCRIPT>
10 string replacement
1: thisvar. Replace ("Monday", "Friday ");
11 format a string
1: <script language = "JavaScript">
2: <! --
3: var myvariable = "Hello there ";
4: Document. Write (myvariable. Big () + "<br> ");
5: Document. Write (myvariable. Blink () + "<br> ");
6: Document. Write (myvariable. Bold () + "<br> ");
7: Document. Write (myvariable. Fixed () + "<br> ");
8: Document. Write (myvariable. fontcolor ("red") + "<br> ");
9: Document. Write (myvariable. fontsize ("18pt") + "<br> ");
10: Document. Write (myvariable. Italics () + "<br> ");
11: Document. Write (myvariable. Small () + "<br> ");
12: Document. Write (myvariable. Strike () + "<br> ");
13: Document. Write (myvariable. sub () + "<br> ");
14: Document. Write (myvariable. Sup () + "<br> ");
15: Document. Write (myvariable. tolowercase () + "<br> ");
16: Document. Write (myvariable. touppercase () + "<br> ");
17:
18: var firststring = "my string ";
19: var finalstring = firststring. Bold (). tolowercase (). fontcolor ("red ");
20: // -->
21: </SCRIPT>
12. Create an array
1: <script language = "JavaScript">
2: <! --
3: var myarray = new array (5 );
4: myarray [0] = "first entry ";
5: myarray [1] = "second entry ";
6: myarray [2] = "third entry ";
7: myarray [3] = "fourth entry ";
8: myarray [4] = "th entry ";
9: var anotherarray = new array ("first entry", "second entry", "third entry", "fourth entry", "th entry ");
10: // -->
11: </SCRIPT>
13. Sort Arrays
1: <script language = "JavaScript">
2: <! --
3: var myarray = new array (5 );
4: myarray [0] = "Z ";
5: myarray [1] = "C ";
6: myarray [2] = "D ";
7: myarray [3] = "";
8: myarray [4] = "Q ";
9: Document. Write (myarray. Sort ());
10: // -->
11: </SCRIPT>
14. Split string
1: <script language = "JavaScript">
2: <! --
3: var myvariable = "a, B, c, d ";
4: var stringarray = myvariable. Split (",");
5: Document. Write (stringarray [0]);
6: Document. Write (stringarray [1]);
7: Document. Write (stringarray [2]);
8: Document. Write (stringarray [3]);
9: // -->
10: </SCRIPT>
15. A warning message is displayed.
1: <script language = "JavaScript">
2: <! --
3: window. Alert ("hello ");
4: // -->
5: </SCRIPT>
16 pop-up confirmation box 1: <script language = "JavaScript">
2: <! --
3: var result = Window. Confirm ("Click OK to continue ");
4: // -->
5: </SCRIPT>
17 define functions
1: <script language = "JavaScript">
2: <! --
3: Function Multiple (number1, number2 ){
4: var result = number1 * number2;
5: return result;
6 :}
7: // -->
8: </SCRIPT>
18. Call JS Functions
1: <a href = "#" onclick = "functionname ()"> link text </a>
2: <a href = "/" javascript: functionname "()"> link text </a>
19. Execute the function after the page is loaded.
1: <body onload = "functionname ();">
2: Body of the page
3: </body>
20 condition judgment
1: <SCRIPT>
2: <! --
3: var userchoice = Window. Confirm ("choose OK or cancel ");
4: var result = (userchoice = true )? "OK": "cancel ";
5: Document. Write (result );
6: // -->
7: </SCRIPT>
21 specified number of cycles 1: <SCRIPT>
2: <! --
3: var myarray = new array (3 );
4: myarray [0] = "item 0 ";
5: myarray [1] = "item 1 ";
6: myarray [2] = "item 2 ";
7: for (I = 0; I <myarray. length; I ++ ){
8: Document. Write (myarray [I] + "<br> ");
9 :}
10: // -->
11: </SCRIPT>
22. set future execution
1: <SCRIPT>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5 :}
6: window. setTimeout ("Hello ()", 5000 );
7: // -->
8: </SCRIPT>
23. Scheduled function execution
1: <SCRIPT>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5: window. setTimeout ("Hello ()", 5000 );
6 :}
7: window. setTimeout ("Hello ()", 5000 );
8: // -->
9: </SCRIPT>
24 cancel scheduled execution
1: <SCRIPT>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5 :}
6: var mytimeout = Window. setTimeout ("Hello ()", 5000 );
7: window. cleartimeout (mytimeout );
8: // -->
9: </SCRIPT>
25. Execute the function when uninstalling the page.
1: <body onUnload = "functionname ();">
2: Body of the page
3: </body>