Javascript-related data collection

Source: Internet
Author: User
Sometimes you are proficient in a language, but you will find that you are dealing with other languages all day long. Maybe you think these are insignificant and will not affect your development progress, however, it is precisely these things that you do not pay attention to that will waste a lot of time. I always thought that I was already proficient in Javascript a few years ago. Until now, I feel that JavaScript is far more complex and powerful than I think. I began to worship it, just like all OOP languages ~

Take advantage of the holiday gap, sort out the Javascript methods and techniques, and let everyone who is worried about JavaScript understand that JavaScript is the case! I also hope that javascript can become your friend, so that you can use JavaScript better in projects ~

Intended audience: No JavaScript knowledge ~ Only one step away from proficient
Basic knowledge: HTML

Javascript: 1. Basic Knowledge

1. Create a script block

1: <script language = "JavaScript">
2: JavaScript code goes here
3: </SCRIPT>

2. Hide the script code

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>

Javascript: 2: browser output

26 access Document Object

1: <script language = "JavaScript">
2: var myurl = Document. url;
3: window. Alert (myurl );
4: </SCRIPT>

27 Dynamic HTML output

1: <script language = "JavaScript">
2: Document. Write ("<p> here's some information about this document: </P> ");
3: Document. Write ("<ul> ");
4: Document. Write ("<li> referring document:" + document. referrer + "</LI> ");
5: Document. Write ("<li> domain:" + document. Domain + "</LI> ");
6: Document. Write ("<li> URL:" + document. url + "</LI> ");
7: Document. Write ("</ul> ");
8: </SCRIPT>

28 output line feed

1: Document. writeln ("<strong> A </strong> ");
2: Document. writeln ("B ");

29. Output date

1: <script language = "JavaScript">
2: var thisdate = new date ();
3: Document. Write (thisdate. tostring ());
4: </SCRIPT>

30 Time Zone of the specified date

1: <script language = "JavaScript">
2: var myoffset =-2;
3: var currentdate = new date ();
4: var useroffset = currentdate. gettimezoneoffset ()/60;
5: var timezonedifference = useroffset-myoffset;
6: currentdate. sethours (currentdate. gethours () + timezonedifference );
7: Document. Write ("the time and date in Central Europe is:" + currentdate. tolocalestring ());
8: </SCRIPT>

31. Set the date output format

1: <script language = "JavaScript">
2: var thisdate = new date ();
3: var thistimestring = thisdate. gethours () + ":" + thisdate. getminutes ();
4: var thisdatestring = thisdate. getfullyear () + "/" + thisdate. getmonth () + "/" + thisdate. getdate ();
5: Document. Write (thistimestring + "on" + thisdatestring );
6: </SCRIPT>

32. Read URL parameters

1: <script language = "JavaScript">
2: var urlparts = Document. url. Split ("? ");
3: var parameterparts = urlparts [1]. Split ("&");
4: for (I = 0; I <parameterparts. length; I ++ ){
5: var pairparts = parameterparts [I]. Split ("= ");
6: var pairname = pairparts [0];
7: var pairvalue = pairparts [1];
8: Document. Write (pairname + ":" + pairvalue );
9 :}
10: </SCRIPT>

Do you still think HTML is stateless?

33 open a new document object

1: <script language = "JavaScript">
2: function newdocument (){
3: Document. open ();
4: Document. Write ("<p> This is a new document. </P> ");
5: Document. Close ();
6 :}
7: </SCRIPT>

34 page Jump

1: <script language = "JavaScript">
2: window. Location = "http://www.liu21st.com /";
3: </SCRIPT>

35 add webpage loading progress window

1: <HTML>
2: 3: <script language = 'javascript '>
4: var placeholder = folder holder open('holder.html ', 'placeholder', 'width = 200, Height = 200 ');
5: </SCRIPT>
6: <title> the main page </title>
7: 8: <body onload = 'placeholder. Close () '>
9: <p> This is the main page </P>
10: </body>
11:

Javascript is like this 3: Images

36 read image attributes

1:
2: <a href = "#" onclick = "window. Alert (document. myimage. width)"> width </a>
3:

37 dynamic image loading

1: <script language = "JavaScript">
2: myimage = new image;
3: myimage. src = maid ";
4: </SCRIPT>

38 simple image replacement

1: <script language = "JavaScript">
2: rollimage = new image;
3: rollimage. src = Your rollimage1.jpg ";
4: defaultimage = new image;
5: defaultimage. src = Invalid image1.jpg ";
6: </SCRIPT>
7: <a href = "/" myurl "" onmouseover = "document. myimage. src = rollimage. SRC ;"
8: onmouseout = "document. myimage. src = defaultimage. SRC;">
9:

39 random Image Display

1: <script language = "JavaScript">
2: var imagelist = new array;
3: imagelist [0] = images image1.jpg ";
4: imagelist [1] = images image2.jpg ";
5: imagelist [2] = image3.jpg ";
6: imagelist [3] = image4.jpg ";
7: var imagechoice = math. Floor (math. Random () * imagelist. Length );
8: Document. Write (' ');
9: </SCRIPT>

40 function-implemented image replacement

1: <script language = "JavaScript">
2: var source = 0;
3: var replacement = 1;
4: function createrollover (originalimage, replacementimage ){
5: var imagearray = new array;
6: imagearray [Source] = new image;
7: imagearray [Source]. src = originalimage;
8: imagearray [replacement] = new image;
9: imagearray [replacement]. src = replacementimage;
10: Return imagearray;
11 :}
12: var rollimage1 = createrollover(images image1.jpg "," rollimage1.jpg ");
13: </SCRIPT>
14: <a href = "#" onmouseover = "document. myimage1.src = rollimage1 [replacement]. SRC ;"
15: onmouseout = "document. myimage1.src = rollimage1 [Source]. SRC;">
16:
17: </a>

41. Create a slide

1: <script language = "JavaScript">
2: var imagelist = new array;
3: imagelist [0] = new image;
4: imagelist [0]. src = images image1.jpg ";
5: imagelist [1] = new image;
6: imagelist [1]. src = images image2.jpg ";
7: imagelist [2] = new image;
8: imagelist [2]. src = image3.jpg ";
9: imagelist [3] = new image;
10: imagelist [3]. src = image4.jpg ";
11: function slideshow (imagenumber ){
12: Document. slideshow. src = imagelist [imagenumber]. SRC;
13: imagenumber + = 1;
14: If (imagenumber <imagelist. Length ){
15: window. setTimeout ("slideshow (" + imagenumber + ")", 3000 );
16 :}
17 :}
18: </SCRIPT>
19: 20: <body onload = "slideshow (0)">
21:

42 random ad Images

1: <script language = "JavaScript">
2: var imagelist = new array;
3: imagelist [0] = images image1.jpg ";
4: imagelist [1] = images image2.jpg ";
5: imagelist [2] = image3.jpg ";
6: imagelist [3] = image4.jpg ";
7: var urllist = new array;
8: urllist [0] = "http://some.host /";
9: urllist [1] = "http://another.host /";
10: urllist [2] = "http://somewhere.else /";
11: urllist [3] = "http://right.here /";
12: var imagechoice = math. Floor (math. Random () * imagelist. Length );
13: document. write ('<a href = "' + urllist [imagechoice] + '"> </a> ');
14: </SCRIPT>

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.