Common JavaScript Functions (III)

Source: Internet
Author: User
4. format String variables <SCRIPT> str1 = "peace, happiness and prosperity. <br> "document. write (str1) document. write (str1.big () document. write (str1.small () document. write (str1.bold () document. write (str1.italics () document. write (str1.strike () document. write (str1.fontsize (6) document. write (str1.fontcolor (green) </SCRIPT> 5. create an anchor and link <SCRIPT> str1 = "this is the bigginning of the page. <br> "str2 = ".... <Br> "str3 =" this is the end of the page. <br> "str4 =" link to the start <br> "str5 =" link to the end <br> "document. write (str1.anchor ("START") for (I = 0; I <10; I ++) document. write (str2); document. write (str3.anchor ("end") document. write (str4.link ("# Start") document. write (str5.link ("# End") </SCRIPT> 6. determine the string length <SCRIPT> str1 = "this is the bigginning of the page. "document. write (str1 + "<br>") document. write ("The string length is:" + str1.length) document. write ("all strings are in uppercase;" + str1.touppercase () document. write ("all strings are in lower case;" + str1.tolowercase () </SCRIPT> 7. search for <SCRIPT> str1 = "this is the end of the line. <br> "document. write (str1) document. write ("the character end is in the string position" + str1.search ("end") document. write ("the character dog is in the string position" + str1.search ("dog") </SCRIPT> 8. locate the character in the string <SCRIPT> str1 = "Spring is a time for flowers and trees and baby bunnl Es <br> "document. write (str1) document. write ("the index for the second word 'and' is" + str1.indexof ("and", 30) receivednt. write ("the last index of the word 'and' is" + str1.lastindexof ("and") </SCRIPT> 9. replace the text in the string <SCRIPT> str1 = "Spring is a time for flowers and trees and baby bunnles <br>" document. write (str1) document. write (str1.replace ("and", ",") </SCRIPT> 10. string separation <SCRIPT> str1 = "Spring is a time For flowers and trees and baby bunnles <br> "document. write (str1) str1array = str1.split ("") document. write (str1array [0] + "<br>") document. write (str1array [1] + "<br>") document. write (str1array [2] + "<br>") document. write (str1array [3] + "<br>") </SCRIPT> Chapter 10 use date and time 1. use the date object <SCRIPT> cdate = new date ("August 12:30:00") document. write (cdate) </SCRIPT> 2. display the local time and date <SCRIPT> cdate = new date () document. WRI Te ("current time is:" + cdate. togmtstring () + "<br>") document. write ("Date and time are:" + cdate. tolocalestring () </SCRIPT> 3. obtain the time and date value <SCRIPT> cdate = new date () document. write ("show current week" + cdate. getday () + "<br>") document. write ("show current month" + cdate. getmonth () + "<br>") document. write ("display current date" + cdate. getday () + "<br>") document. write ("display current year" + cdate. getyear () + "<br>") document. write ("show current hour" + cdate. gethours () + "<br>") document. write ("show when Minutes earlier "+ cdate. getminutes () + "<br>") document. write ("display current second" + cdate. getseconds () + "<br>") </SCRIPT> 4. set the time and date value <script language = JavaScript> cdate = new date ("December 25, 1984") document. write ("display date" + cdate + "<br>") document. write ("set month" + cdate. setmonth (10) + "<br>") document. write ("set date" + cdate. setdate (23) + "<br>") document. write ("set year" + cdate. setyear (2000) + "<br>") document. write ("set hour" + cdate. sethours (13) + "<br> "); Document. write ("set minute" + cdate. setminutes (47) + "<br>"); document. write ("set second" + cdate. setseconds (23) + "<br>"); document. write ("display the date and time after setting" + cdate); </SCRIPT> Chapter 11th use math object 1. use the <script language = JavaScript> </SCRIPT> <form name = form1> circle radius of the math object: <input type = text name = rad> <br> area of the circle: <input type = text name = Area> <br> <input type = button name = button1 value = Area of the calculated circle onclick = document. form1.area. value = document. form 1. rad. value * document. form1.rad. value * Math. PI> </form> 2. generate a random number <SCRIPT> array1 = new array ("This is the 1st sentence", "This is the 2nd sentence", "This is the 3rd sentence", "this is the 4th sentence ", "This is the 6th sentence") randomno = math. floor (array1.length * Math. random () document. write ("random output of a sentence" + "<br>" + array1 [randomno]) </SCRIPT> 3. use the square root <form name = form1> value: <input type = text name = va1> <br> Square Root <input type = text name = SQRT> <br> <input type = button name = button1 value = calculate the square root of oncli Ck = "document. form1.sqrt. value = math. SQRT (document. form1.va1. value) "> </form> 4. number rounding <form name = form1> input <input type = text name = Val> <br> rounding result <input type = text name = round> <br> <input type = button name = button1 value = calculation result onclick = document. form1.round. value = math. round (document. form1.val. value)> </form> 5. multiplication operator <form name = form1> base number <input type = text name = Val> <br> index <input type = text name = power> <br> power <input ty Pe = text name = Result> <br> <input type = button name = button1 value = calculation result onclick = "document. form1.result. value = math. pow (document. form1.val. value, document. form1.power. value) "> </form> 6. the minimum and maximum values are found. <form name = form1> Number 1 <input type = text name = val1> <br> Number 2 <input type = text name = val2> <br> Minimum value <input type = text name = min> <br> maximum value <input type = text name = max> <br> Number 1 <input type = button value = calculate onclick = "document. form1. Min. value = math. min (document. form1.val1. value, document. form1.val2. value); document. form1. Max. value = math. max (document. form1.val1. value, document. form1.val2. value) "> </form> Chapter 12th use Form 1. use the text box <form name = form1> <input type = text value = "information, please" name = text1> </form> <SCRIPT> document. write ("form text1 type:" + document. form1.text1. type + "<br>") document. write ("form text1 name is:" + document. form1.text1. nam E + "<br>") document. write ("the form text1 value is:" + document. form1.text1. value + "<br>") document. write ("the size of form text1 is:" + document. form1.text1. size + "<br>") </SCRIPT> <form name = form1> <input type = text name = text1 value = click here onfocus = document. form1.text1. select ()> </form> 2. use the Password box <form name = form1> <input type = password name = pw1 value = daylight> </form> <SCRIPT> document. write ("type of form pw1:" + document. form1.pw1. type + "< BR> ") document. write ("Name of form pw1:" + document. form1.pw1. name + "<br>") document. write ("value of form pw1:" + document. form1.pw1. value + "<br>") document. write ("size of form pw1:" + document. form1.pw1. size + "<br>") </SCRIPT> 3. use the hidden field <form name = form1> <input type = hidden name = hid1 value = piece of eight> </form> <SCRIPT> document. write ("type of form hid1:" + document. form1.hid1. type + "<br>") document. write ("form hid1 name:" + document. form1.hid1. nam E + "<br>") document. write ("value of form hid1:" + document. form1.hid1. value + "<br>") </SCRIPT> 4. use the text box <form name = form1> <textarea name = TA1> How does grains of sand are there in the Sahara Desert? </Textarea> </form> <SCRIPT> document. write ("type of form TA1:" + document. form1.ta1. type + "<br>") document. write ("form TA1 name:" + document. form1.ta1. name + "<br>") document. write ("value of form TA1:" + document. form1.ta1. value + "<br>") document. write ("horizontal width of form TA1:" + document. form1.ta1. cols + "<br>") document. write ("vertical width of form TA1:" + document. form1.rows. value + "<br>") </SCRIPT> 6. use the reset button <form name = form1> <input type = reset name = reset1 valu E = "rest form"> </form> <SCRIPT> document. write ("form reset1 type:" + document. form1.reset1. type + "<br>") document. write ("form reset1 name:" + document. form1.reset1. name + "<br>") document. write ("value of form reset1:" + document. form1.reset1. value + "<br>") </SCRIPT> 7. use the submit button <form name = form1> <input type = submit name = submit1 value = "Submit Form"> </form> <SCRIPT> document. write ("type of form submit1:" + document. form1.submit1. type + "<br> ") Document. write ("Name of form submit1:" + document. form1.submit1. name + "<br>") document. write ("value of form submit1:" + document. form1.submit1. value + "<br>") </SCRIPT> 8. use the check button <form name = form1> <input type = checkbox name = CB1> computer savvy? </Form> <SCRIPT> document. write ("type of form CB1:" + document. form1.cb1. type + "<br>") document. write ("is form CB1 selected?: "+ Document. form1.cb1. checked + "<br>") document. write ("form CB1 name:" + document. form1.cb1. name + "<br>") </SCRIPT> 9. use the single-choice button <form name = form1> <input type = radio name = radio1> male <input type = radio name = radio1> female </form> <SCRIPT> document. write ("the first button is selected" + document. form1.radio1 [0]. checked + "<br>") document. write ("the second button is selected" + document. form1.radio1 [1]. checked + "<br>") document. write ("button name" + document. form1.radio 1 [0]. name + "<br>") document. write ("Number of buttons" + document. form1.radio1. length) </SCRIPT> 10. use the selection list <form name = form1> <select name = select1 size = 4> <option name = option1 value = Lon> LONDON, england </option> <option name = option2 value = dub> Dublin, Ireland </option> </SELECT> </form> <SCRIPT> document. write ("Name of the selection list" + document. form1.select1. name + "<br>") document. write ("the length of the selected list" + document. form1.select1. length + "<br> ") Document. write ("the selected index number in this selection list" + document. form1.select1. selectedindex + "<br>") document. write ("Size of the selected list" + document. form1.select1. size + "<br>") </SCRIPT> 11. verify the validity of the form <SCRIPT> function validate () {If (document. form1.text1. value! = '1' | '2' | '3' | '4') {alert ("Enter 1 ~ 4 integer ") }}</SCRIPT> <form name = form1> enter 1 ~ Integer 4: <input type = text name = text1 size = 4 onchange = validate ()> </form> 12. control Form focus <form name = form1> <input type = text name = text1 value = Where is you focus?> <Br> <input type = text name = text2 value = is there?> <Br> <input type = text name = text3 value = or maybe here?> <Br> <input type = button name = button1 value = "text box #1" onclick = document. form1.text1. focus ()> <br> <input type = button name = button2 value = "text box #2" onclick = document. form1.text2. focus ()> <br> <input type = button name = button3 value = "text box #3" onclick = document. form1.text3. focus ()> <br> </form> Chapter 13th use sub-column chapter 14th use navigator 1. use the navigator object <SCRIPT> document. write ("attributes of the navigator object" + "<br>") document. write ("Appcodename:" + navigator. appcodename + "<br>") document. write ("appname:" + navigator. appname + "<br>") document. write ("appversion:" + navigator. appversion + "<br>") document. write ("Platform:" + navigator. platform + "<br>") document. write ("useragent:" + navigator. useragent + "<br>") </SCRIPT> <SCRIPT> document. write ("method of the navigator object" + "<br>") document. write ("javaenabled ():" + navigator. javaenabled () </SCRIPT> 2. check the user's Browser <SCRIPT> If (navigator. appname. indexof ("Microsoft ")! =-1) {document. Write ("your browser is Microsoft's IE browser" + "<br>")} else if (navigator. appname. indexof ("Netscape ")! =-1) {document. Write ("your browser is Netscape's Netscape Browser" + "<br>")} If (navigator. appversion. indexof ("4.0 ")! =-1) {document. write ("you are using a version 4.0 compatible Browser")} else {document. write ("This browser is not 4.0 compliant")} </SCRIPT> 3. detects the user's operating system <SCRIPT> If (navigator. platform. indexof ("Win32 ")! =-1) {document. write ("you are using a computer running Windows 95 or highter")} else {document. write ("this computer is not running Windows 95 or higher")} </SCRIPT> 4. use location object <SCRIPT> document. write ("location object attributes" + "<br>") document. write ("hash" + location. hash + "<br>") document. write ("hostname" + location. hostname + "<br>") document. write ("host" + location. host + "<br>") document. write ("href" + location. HR EF + "<br>") document. write ("Port" + location. port + "<br>") document. write ("Search" + location. search + "<br>") </SCRIPT> reload the webpage <form name = form1> <input type = button name = button1 value = reload this page onclick = location. reload> </form> 5. use cookie <SCRIPT> finction makecookie () {If (! Document. cookie) {name = prompt ("enter your name"); document. cookie = "name =" + name + ";" ;}}</SCRIPT> <body onload = makecookie ()> <SCRIPT> function makecookie () {If (! Document. cookie) {name = prompt ("enter your name") document. cookie = "name =" + name + ";"; namestart = document. cookie. indexof ("="); nameend = document. cookieindexof (";"); document. writeln ("Your name is:" + document. cookie. substring (namestart + 1, nameend) + ", Br>") }}</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.