Methods and Techniques for using JavaScript (2)

Source: Internet
Author: User

21 specified number of cycles

1: <s timeout>
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: </S release>

22. set future execution

1: <s timeout>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5 :}
6: window. setTimeout ("Hello ()", 5000 );
7: // -->
8: </s timeout>

23. Scheduled function execution

1: <s timeout>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5: window. setTimeout ("Hello ()", 5000 );
6 :}
7: window. setTimeout ("Hello ()", 5000 );
8: // -->
9: </s success>

24 cancel scheduled execution

1: <s timeout>
2: <! --
3: function Hello (){
4: window. Alert ("hello ");
5 :}
6: var mytimeout = Window. setTimeout ("Hello ()", 5000 );
7: window. cleartimeout (mytimeout );
8: // -->
9: </s success>

25. Execute the function when uninstalling the page.

1: <body onUnload = "functionname ();">
2: Body of the page
3: </body>

This is true for javas outputs. 2. browser output.

26 access Document Object

1: <s required language = "javas alias">
2: var myurl = Document. url;
3: window. Alert (myurl );
4: </s timeout>

27 Dynamic HTML output

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

28 output line feed

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

29. Output date

1: <s required language = "javas alias">
2: var thisdate = new date ();
3: Document. Write (thisdate. tostring ());
4: </s timeout>

30 Time Zone of the specified date

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

31. Set the date output format

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

32. Read URL parameters

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

Do you still think HTML is stateless?

33 open a new document object

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

34 page Jump

1: <s required language = "javas alias">
2: window. Location = http://www.liu21st.com /";
3: </s timeout>

35 add webpage loading progress window

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

Javas pull 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: <s required language = "javas alias">
2: myimage = new image;
3: myimage. src = maid ";
4: </s timeout>

38 simple image replacement

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

39 random Image Display

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

40 function-implemented image replacement

1:
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 :}< br> 12: var rollimage1 = createrollover({image1.jpg}{rollimage1.jpg ");
13:
14: 15: onmouseout =" document. myimage1.src = rollimage1 [Source]. SRC; ">
16:
17:

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.