Methods and Techniques for using JavaScript (3)

Source: Internet
Author: User

41. Create a slide

1: <s required language = "javas alias">
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: </s success>
19: 20: <body onload = "slideshow (0)">
21:

42 random ad Images

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 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: </s success>

4: Form

Continue writing JavaScript ~
43 form composition

1: <form method = "Post" action=”target.html "name =" thisform ">
2: <input type = "text" name = "mytext">
3: <select name = "myselect">
4: <option value = "1"> first choice </option>
5: <option value = "2"> second choice </option>
6: </SELECT>
7: <br/>
8: <input type = "Submit" value = "submit me">
9: </form>

44 access the text box content in the form

1: <form name = "myform">
2: <input type = "text" name = "mytext">
3: </form>
4: <a href = '# 'onclick = 'window. Alert (document. myform. mytext. Value);'> check text field </a>

45. dynamically Copy text box content

1: <form name = "myform">
2: Enter some text: <input type = "text" name = "mytext"> <br/>
3: Copy text: <input type = "text" name = "copytext">
4: </form>
5: <a href = "#" onclick = "document. myform. copytext. value =
6: Document. myform. mytext. value; "> Copy text field </a>

46. Detect text box changes

1: <form name = "myform">
2: Enter some text: <input type = "text" name = "mytext" onchange = "alert (this. Value);">
3: </form>

47. Access the selected select

1: <form name = "myform">
2: <select name = "myselect">
3: <option value = "First Choice"> 1 </option>
4: <option value = "second choice"> 2 </option>
5: <option value = "third choice"> 3 </option>
6: </SELECT>
7: </form>
8: <a href = '# 'onclick = 'alert (document. myform. myselect. Value);'> check selection list </a>

48 dynamically add select items

1: <form name = "myform">
2: <select name = "myselect">
3: <option value = "First Choice"> 1 </option>
4: <option value = "second choice"> 2 </option>
5: </SELECT>
6: </form>
7: <s required language = "javas alias">
8: Document. myform. myselect. Length ++;
9: Document. myform. myselect. Options [document. myform. myselect. Length-1]. Text = "3 ";
10: Document. myform. myselect. Options [document. myform. myselect. Length-1]. value = "third choice ";
11: </S release>

49. verification form fields

1: <s required language = "javas alias">
2: function checkfield (field ){
3: If (field. value = "") {
4: window. Alert ("You must enter a value in the field ");
5: field. Focus ();
6 :}
7 :}
8: </s timeout>
9: <form name = "myform" action=”target.html ">
10: Text Field: <input type = "text" name = "myfield" onblur = "checkfield (this)">
11: <br/> <input type = "Submit">
12: </form>

50 verify select items

1: function checklist (selection ){
2: If (selection. Length = 0 ){
3: window. Alert ("You must make a selection from the list .");
4: Return false;
5 :}
6: Return true;
7 :}

51 dynamically changing the form action

1: <form name = "myform" action=”login.html ">
2: Username: <input type = "text" name = "username"> <br/>
3: Password: <input type = "password" name = "password"> <br/>
4: <input type = "button" value = "login" onclick = "This. Form. Submit ();">
5: <input type = "button" value = "register" onclick = "This. Form. Action = 'register.html '; this. Form. Submit ();">
6: <input type = "button" value = "retrieve password" onclick = "This. Form. Action = 'password.html '; this. Form. Submit ();">
7: </form>

52 use image buttons

1: <form name = "myform" action=”login.html ">
2: Username: <input type = "text" name = "username"> <br/>
3: Password: <input type = "password" name = "password"> <br/>
4: <input type = "image" src = "/”login.gif" "value =" login ">
5: </form>
6:

53. Form Data Encryption

1: <S release language = 'javas upload'>
2: <! --
3: function encrypt (item ){
4: var newitem = '';
5: for (I = 0; I <item. length; I ++ ){
6: newitem + = item. charcodeat (I) + '.';
7 :}
8: Return newitem;
9 :}
10: function encryptform (myform ){
11: for (I = 0; I <myform. elements. length; I ++ ){
12: myform. elements [I]. value = encrypt (myform. elements [I]. value );
13 :}
14 :}
15:
16: // -->
17: </s success>
18: <form name = 'myform' onsubmit = 'encryptform (this); window. Alert (this. myfield. Value); '>
19: Enter some text: <input type = text name = myfield> <input type = submit>
20: </form>

 

This is true for javas pipelines. 5. Windows and frameworks.

54. Text prompt for changing the browser Status Bar

1: <s required language = "javas alias">
2: window. Status = "a new status message ";
3: </s timeout>

55 pop-up confirmation box

1: <s required language = "javas alias">
2: var userchoice = Window. Confirm ("Click OK or cancel ");
3: If (userchoice ){
4: Document. Write ("You chose OK ");
5:} else {
6: Document. Write ("You chose cancel ");
7 :}
8: </s timeout>

56 prompt Input

1: <s required language = "javas alias">
2: var username = Window. Prompt ("Please enter your name", "enter your name here ");
3: Document. Write ("Your name is" + username );
4: </s timeout>

57 open a new window

1: // open a new browser window named mynewwindow
2: <s required language = "javas alias">
3: window. Open (http://www.liu21st.com/#,”mynewwindow ");
4: </s timeout>

58. Set the size of the new window

1: <s required language = "javas alias">
2: window. Open (http://www.liu21st.com/?, mynewwindow=,'height=300,width=300 ');
3: </s timeout>

59. Set the position of the new window

1: <s required language = "javas alias">
2: window. Open (rows ');
3: </s timeout>

60 show toolbar and scroll bar

1: <s required language = "javas alias">
2: window. Open ("http:

61. Can I scale the size of the new window?

1: <s required language = "javas alias">
2: window. Open (http://www.liu21st.com/', 'mynewwindow', 'resizable = yes'); </s success>

62 load a new document to the current window

1: <a href = '# 'onclick = 'document. Location = '125a.html'; '> open new document </a>

63 set the scroll position of the page

1: <s required language = "javas alias">
2: If (document. All) {// if it is an IE browser, use the scrolltop attribute
3: Document. Body. scrolltop = 200;
4:} else {// if it is a Netscape browser, use the pageyoffset attribute
5: window. pageyoffset = 200;
6 :}</s success>

64. Open the full screen window in IE

1: <a href = '# 'onclick = "window. Open (http://www.juxta.com/', 'newwindow', 'fullscreen#yes');"> open a full-screen window </a>

65 operations on new windows and parent windows

1: <s required language = "javas alias">
2: // define a new window
3: var newwindow = too many open(2017128a.html "," newwindow ");
4: newwindow. Close (); // close the new window opened in the parent window
5: </s success>
6. Close the parent window in the new window.
7: window. opener. Close ()

66. Write content to the new window

1: <s required language = "javas alias">
2: var newwindow = Window. Open ("", "newwindow ");
3: newmediaworkflow Doc ument. open ();
4: newmediaworkflow Doc ument. Write ("This is a new window ");
5: newclientdoc ument. Close ();
6: </s timeout>

67 load the page to the Framework page

1: <frameset Cols = "50%, *">
2: <frame name = "frame1" src = "/425135a.html" ">
3: <frame name = "frame2" src = "/" about: blank "">
4: </frameset>
5. Load the page in frame2 in frame1.
6: parent.frame2.doc ument. Location = pai135b.html ";

68 share scripts between framework pages
If the HTML file in frame1 contains a script

1: function doalert (){
2: window. Alert ("frame 1 is loaded ");
3 :}

This method can be called in frame2.

1: <body onload = "parent. frame1.doalert ();">
2: This is frame 2.
3: </body>

69 public data
You can define a data item on the Framework page so that the data can be shared by pages in multiple frameworks.

1: <s required language = "javas alias">
2: var persistentvariable = "This is a persistent value ";
3: </s timeout>
4: <frameset Cols = "50%, *">
5: <frame name = "frame1" src = "/425138a.html" ">
6: <frame name = "frame2" src = "/”138b.html" ">
7: </frameset>

In this way, the variable persistentvariable can be used in both frame1 and frame2.
70 FrameCodeLibrary
Based on the above ideas, we can use a hidden framework page as the code base of the entire framework set.

1: <frameset Cols = "0, 50%, *">
2: <frame name = "codeframe" src = "/”140code.html" ">
3: <frame name = "frame1" src = "/”140a.html" ">
4: <frame name = "frame2" src = "/fig" ">
5: </frameset>

Related Article

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.