Methods and Techniques for using JavaScript (1)

Source: Internet
Author: User

1. Create a script block

1: <s required language = "javas alias">
2: javas coding code goes here
3: </s timeout>

 

2. Hide scriptsCode

1: <s required language = "javas alias">
2: <! --
3: Document. Write ("hello ");
4: // -->
5: </s success>

Related code is not executed in browsers that do not support javas libraries.

3 display when the browser does not support

1: <Nos timeout>
2: Hello to the non-javas release browser.
3: </NOS timeout>

 

4. Link an external script file

1: <S release language = "javas release" src = "/" filename. js "> </S release>

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: <s required language = "javas alias">
2: <! --
3: var myvariable = "Hello there ";
4: var thereplace = myvariable. Search ("There ");
5: Document. Write (thereplace );
6: // -->
7: </s timeout>

 

10 string replacement

1: thisvar. Replace ("Monday", "Friday ");

11 format a string

1: <s required language = "javas alias">
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: </s timeout>

 

12. Create an array

1: <s required language = "javas alias">
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: </S release>

 

13. Sort Arrays

1: <s required language = "javas alias">
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: </S release>

 

14. Split string

1: <s required language = "javas alias">
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: </s success>

 

15. A warning message is displayed.

1: <s required language = "javas alias">
2: <! --
3: window. Alert ("hello ");
4: // -->
5: </s success>

 

16 pop-up confirmation box

1: <s required language = "javas alias">
2: <! --
3: var result = Window. Confirm ("Click OK to continue ");
4: // -->
5: </s success>

 

17 define functions

1: <s required language = "javas alias">
2: <! --
3: Function Multiple (number1, number2 ){
4: var result = number1 * number2;
5: return result;
6 :}
7: // -->
8: </s timeout>

 

18. Call JS Functions

1: <a href = "#" onclick = "functionname ()"> link text </a>
2: <a href = "/" javas alias: 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:
2: 3: var userchoice = Window. Confirm ("Choose OK or cancel");
4: var result = (userchoice = true )? "OK": "cancel";
5: document. write (result);
6: // -->
7:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.