More than a series of javascript

Source: Internet
Author: User
Tags expression functions hash variables sort square root variable time and date
JavaScript

Tsinghua University published the "Half a series of JavaScript", I read the book, some translated it. A few years ago looked, recently nothing, turned over, very helpful. This book should have CD-ROM, but the school book, the CD-ROM is not known. I hope it helps you learn JavaScript.

The first chapter of JavaScript introduction

1. Enter a JavaScript statement in the address bar

Javascript:Document.write ("Show text")

2. Embed JavaScript in an HTML document

<script language=javascript>
Document.bgcolor= "Blue."
</script>

Chapter II using Variables and arrays

1. Declaring variables

<script language=javascripe>
Var Answer1,answer2,answer3,answer4;
answer1=9;
answer2=2.5
answer3= "Milkey May"
Answer4=true
</script>

2. Using integer

<script language=javascript>
var decimalnum,hexadecimalnum,octalnum
Decimalnum=24
hexadecimalnum=0x24
octalnum=024
document.write ("Display decimal number:" + decimalnum+ "<br>")
document.write ("Display hexadecimal number:" + hexadecimalnum + "<br>")
document.write ("Show octal number:" + octalnum + "<br>")
</script>

3. Using floating point numbers

<script language=javascript>
var num1,num2,num3,num4
num1=1234567890000.0
Num2=5.14e23
num3=0.0000123456
Num4=6.0254e3-4
document.write ("Floating-point number 1:" +num1+ "<br>")
document.write ("Floating-point number 2:" +num2+ "<br>")
document.write ("Floating-point number 3:" +num3+ "<br>")
document.write ("Floating-point number 4:" +num4+ "<br>")
</script>

4. Using Boolean values

<script language=javascript>
var answer1,answer2
Answer1=true
Answer2=false
document.write ("Display Boolean 1:" +answer1+ "<br>")
document.write ("Display Boolean 2:" +answer2+ "<br>")
</script>

5. Using a String

<script language=javascript>
var str1,str2
str1= "FDSGDG DSFDSF"
Str2= "Wuhan Rtvu"
document.write ("Display string 1:" +str1+ "<br>")
document.write ("Display string 2:" +str2+ "<br>")
</script>

6. Determine variable type

<script>
var answer1,answer2,answer3,answer4
Answer1=9
answer2=2.5
Answer3= "Milky May"
Answer4=true
document.write ("The type of variable 1 is:" +typeof answer1 + "<br>")
document.write ("The type of variable 2 is:" +typeof answer2 + "<br>")
document.write ("The type of variable 3 is:" +typeof Answer3 + "<br>")
document.write ("The type of variable 4 is:" +typeof answer4 + "<br>")
</script>

7. Convert a string into a number

<script>
var str1= "in January"
var int1=parseint (STR1)
document.write ("str1 data type is:" +typeof str1+ "<br>")
document.write ("Int1 data type is:" +typeof int1+ "<br>")
</script>

8. Convert numbers to Strings

<script>
var int1=256
var str1= "" +int1
document.write ("str1 data type is:" +typeof str1+ "<br>")
document.write ("Int1 data type is:" +typeof int1+ "<br>")
</script>

9. Declaring an array

<script>
Array=new Array (5)
Array[0]=1
Array[1]=3
Array[2]=5
Array[3]=7
array[4]=11
document.write ("Array is:" +array[0]+ "" +array[1]+ "" +array[2]+ "" +array[3]+ "" +array[4])
</script>

10. Determine the number of array elements

<script>
Array=new Array (5)
Array[0]=1
Array[1]=3
Array[2]=5
Array[3]=7
array[4]=11
document.write ("Array is:" +array[0]+ "" +array[1]+ "" +array[2]+ "" +array[3]+ "" +array[4]+ "<br>")
document.write ("The number of elements of an array is" +array.length)
</script>

11. Convert an array to a string

<script>
Array=new Array ()
array[0]= "Dark"
array[1]= "Apple"
array[2]= "Nebula"
array[3]= "Water"
Str1=array.join ()
Str2=array.join ("")
document.write (str1+ "<br>")
document.write (STR2)
</script>

12. Sorting arrays

<script>
Array=new Array ()
array[0]= "Dark"
array[1]= "Apple"
array[2]= "Nebula"
array[3]= "Water"
Str1=array.sort ()
document.write (str1+ "<br>")
</script>

Chapter Three Creating an expression

1. Using Arithmetic operators

<script>
Var1=12
var2=10
Varadd=var1+var2
Varsub=var1-var2
Varmult=var1*var2
Vardiv=var1/var2
Varmod=var1%var2
document.write ("Data 1 is:" +var1+ "<br>")
document.write ("Data 2 is:" +var2+ "<br>")
document.write ("Add data is:" +varadd+ "<br>")
document.write ("Data subtraction is:" +varsub+ "<br>")
document.write ("Data multiplication is:" +varmult+ "<br>")
document.write ("Dividing data is:" +vardiv+ "<br>")
document.write ("Data division remainder is:" +varmod+ "<br>")
</script>

2. Increment variable and decrement variable

<script>
Days=1
document.write ("Output variable" +days+ "<br>")
days++
document.write ("Incremental variable becomes:" +days)
</script>

3. Create a comparison expression

<script>
Daysofmonth=28
if (daysofmonth==28)
Month= "February"
document.write ("Days of Month:" +daysofmonth+ "<br>")
document.write ("Month:" +month)
</script>

4. Creating Logical Expressions

<script>
Dayofmonth=28
if (dayofmonth==28 | | dayofmonth==29)
Month= "February"
document.write ("Days of Month:" +dayofmonth+ "<br>")
document.write ("Month:" +month)
</script>

5. Using the conditional operator

<script language= "JavaScript" >
Stomach= "Hungry";
Time= "5:00";
(stomach== "Hungry" &&time== "5:00")? Eat = "Dinner": eat= "a snack";
document.write ("output result" +eat);
</script>

6. Identifying numbers

<script>
var1=24;
(isNaN (var1))? document.write ("Variable var1" +var1+ "not Number"):D ocument.write ("Variable var1" +var1+ "is number")
</script>

Fourth Chapter control procedure Flow

1. Using the If–else statement

<script>
Month= "December"
Date=25
if (month== "December" && date==25)
document.write ("Today is Christmas, shop closes")
Else
document.write ("Welcome, you come to the store to shop")
</script>

2. Use for loop

<script>
for (count=1;count<=10;count++)
document.write ("Output" +count+ "" + "<br>")
</script>

3. Use while loop

<script>
Count=1
while (count<=15) {
document.write ("Output" +count+ "" + "<br>")
count++}
</script>

4. Interrupt Cycle

<script>
Count=1
while (count<=15) {
count++
if (count==8)
Break
document.write ("Output first" +count+ "sentence" + "<br>")}
</script>

5. Continue the Cycle

<script>
Count=1
while (count<=15) {
count++
if (count==8)
Continue
document.write ("Output first" +count+ "sentence" + "<br>")}
</script>

6. Use JavaScript timer

<script>
function Rabbit ()
{document.write ("output statement")
}
</script>
<body onload=window.settimeout (Rabbit (), 5000) >

7. Set the recurrence interval

<script>
Window.setinterval ("Document.form1.text2.value=document.form1.text1.value", 3000)
</script>
<form name=form1>
<input Type=text name=text1><br>
<input Type=text name=text2><br>
</form>

8. Purge Timeout and interval

<script>
Stop=window.setinterval ("Document.form1.text2.value=document.form1.text1.value", 300)
</script>
<form name=form1>
<input Type=text name=text1><br>
<input Type=text name=text2><br>
<input Type=button name=button1 value= "purge timeout and interval" onclick=clearinterval (stop) >
</form>

The fifth chapter uses the function

1. Declaring functions

<script>
function quote ()
{document.write ("output statement")
}
</script>

2. Call function

<script>
function quote ()
{document.write ("output statement")
}
QUOTE ()
</script>

3. Understanding global variables and local variables

Any variable declared without the var keyword is a global variable, and any variable declared outside the function is a global variable

4. Transfer parameters to function

<script>
function f (item)
{document.write ("Output parameter" +item+ "<br>")
}
F ("FGDFGD")
F ("parameter Two")
</script>

5. return value from function

<script>
function average (VAR1,VAR2,VAR3)
{ave= (VAR1+VAR2+VAR3)/3;
document.write ("output result");
Return Ave;
}
document.write (average (34,56,78))
</script>

6. Calling functions through HTML links

<script>
function quote () {
document.write ("Output string")
}
</script>
<a href=javascript:quote () > Calling functions via HTML link </a>
<a href=javascript:document.write ("output character") > Call function via HTML link, write JavaScript statement directly </a>

Chapter sixth dealing with events

1. Check the mouse click

<form name=form1>
<input Type=button name=button1 Value=hello onclick=document.form1.button1.value= ' there ' >
</form>

2. Detect Double click

<form name=form1>
<input Type=button name=button1 value=hello onclick=document.form1.button1.value= ' You clicked the button ' ondblclick= Document.form1.button1.value= ' You double-click the button ' >
</form>

3. Create a hover button

4. Test key

<form name=form1>
<input type=text name=text1 Value=hello onkeypress= "if (window.event.keycode== ') document.form1.text1.value= '" You pressed the D key ' ">
</form>

5. Set Focus

<form name=form1>
<input Type=text Name=text1 Value=hello
Onfous=document.form1.text1.value= ' This text box gets focus '
Onblur=document.form1.text1.value= ' The text box loses focus ' >
</form>

6. Detect Pull-down Menu Selection

<form name=form1>
<select Name=select1 size=4
Onchange=document.form1.text1.value=document.form1.select1.value>
<option value= "Beijing" > Beijing </option>
<option value= "Shanghai" > Shanghai </option>
<option value= "Wuhan" > Wuhan </option>
<option value= "Tianjin" > Tianjin </option>
<option value= "Dalian" > Dalian </option>
</select>
<input Tppe=text Name=text1 value=hello>
</form>

7. Create Web loading and unloading information

<body onload=document.form1.text1.value= ' page load complete ' Onunload=alert (' Goodbye, Welcome again ') >
<form name=form1>
<input type=text name=text1 value= "page is loading ..." >
</form>

Chapter seventh the use of objects

1. Understanding objects \ Properties and methods

<body bgcolor= "Green" >
<script>
document.write ("Page background color is:" +document.bgcolor)
document.write ("page foreground color is:" +document.fgcolor)
</script>

2. Using page Element objects

<script>
</script>
<form name=form1>
<textarea name=ta1>dfgfdgfdhfdhdfdfgdf</textarea>
<input Type=button value= "Select text" Onclick=document.form1.ta1.select () >
<input Type=button value= "display text" Onclick=document.write (document.form1.ta1.value) >
</form>

3. Using child objects


<form name=form1>
<input Type=text Name=text1 value=hello>
</form>
<script>
Document.form1.text1.value= "GDFGFD"
</script>

<form name=form1>
<input Type=radio name=radio1> Male
<input Type=radio name=radio2> Female
</script>
<script>
Document.form1.radio1.checked=true
</script>

4. Using Predefined objects

<script>
Str1= "Dgdfgdfgdfhf fixation method for martial arts attack"
document.write (str1+ "<br>")
STR2=STR1.SUBSTR (5)
document.write (str2+ "<br>")
document.write ("area of the output circle:" +math.pi*math.pow (5.0,2))
</script>

5. Create a new object

<script>
Today=new Date ()
document.write ("Today is" + (Today.getmonth () +1) + "month" +today.getdate () + "Day" + "<br>")
document.write ("Now Is:" +today.tolocalestring ())
</script>

6. Referencing the current object

<form name=form1>
<input type=text name=text1 value= "DGDGDFGFD" Onclick=this.select () >
</script>

7. View Object Properties

<script>
For (prop in window)
{document.write ("window.") +prop+ "=" +window[prop]+ "<br>");}
For (Prop2 in location)
{document.write ("location.") +prop2+ "=" +location[prop]+ "<br>");}
</script>

8. Use the Array object

<script>
Array=new Array (10)
array[0]= "Bark"
array[1]= "Apple"
array[2]= "Nebula"
array[3]= "Cookies"
array[4]= "Technology"
document.write (the number of array elements is +array.) length+ "<br>")
document.write (merge array with join +array.join ("") + "<br>")
document.write ("Array Sort" +array.sort ())
</script>

9. Using the Image Object


<script>
document.write ("Photo tip is:" +document.images[0].alt+ "<br>")
document.write ("Picture border size is:" +document.images[0].broder)
</script>

10. Pre-loading image

<script>
Freddy=new Image ()
Freddy.src=freddy.gif
</script>
<body onload=document.images[0].src=freddy.src>
,
</body>

11. Change the image

<br>
<form name=form1>
<input Type=button name=button1 value= "Change image" onclickd=document.images[0].src=dudjp.gif>
</form>

12. Use Link and Anchor objects

<a name=anchor1> Anchor Point 1<br>
<a href=http://www.microsoft.com>microsoft</a><br>
<a href=http://www.sohu.com>sohu</a><br>
<a href=http://www.sina.com.cn>sina</a><br>
<script>
document.write ("This page has a total of" +document.links.length+ "link" + "<br>")
document.write ("This page has a total of" +document.anchors.length+ "anchor" + "<br>")
document.write ("First link agreement is" +document.links[0].protocol+ "<br>")
document.write ("First link path is" +document.links[0].pathnamel+ "<br>")
document.write ("First link href is" +document.links[0].hrefl+ "<br>")
</script>

13. Change Links

<a href =http://www.microsoft.com>link</a>
<form name=form1>
<input Type=button name=button1 value= "Change link" onclick=document.links[0].href= ' http://www.sohu.com ' >
</form>

14. Using the History object

<form name=form1>
<input Type=button name=button1 value= "Back 2 page" Onclick=window.history.go ( -2) >
</form>

Chapter Eighth using Windows

1. Display text on the status bar of the browser

<body onload=window.status= "Welcome to My Site" >
<a href=http://www.sohu.com>sohu</a>
</body>

2. Change the background color

<script>
Document.bgcolor= "Orange"
</script>

3. List background color

<body bgcolor =green>
<script>
document.write ("Current background color is:" +document.bgcolor)
</script>
</body>

4. Change text and link colors

<script>
Document.bgcolor= "Orange"
Document.fgcolor= "Blue."
Document.linkcolor= "Red"
</script>
<a href=http://www.sohu.com>sohu</a>
</body>

5. Change the title of the document

<script>
Name= "Mouse"
Document.title= "Welcome to" +name+ "' s house"
document.write (Document.title)
</script>

6. Show Modification Date

<script>
document.write ("Last modified on this page is" +document.lastmodified)
</script>

7. View the URL of the current document

<script>
document.write ("URL of this page:" +document.) URL)
</script>

8. View References page

<script>
document.write ("Reference page of this page is" +document.referrer)
</script>

9. Open a new browser window

<script>
window.open ("*.htm", "title", "Width=200,height=400,resizable=yes")
</script>

10. Close the remote window


Close.html:
<script>
document.write ("Body")
</script>
<form name=form1>
<input Type=button name=button1value= "Off" onclick=window.close () >
</form>

Open.html
<script>
window.open ("close.html", "Romote", "Width=200,height=400,resizable=yes")
</script>

11. Print Window

<script>
document.write ("Body")
</script>
<form name=form1>
<input Type=button value= Print onclick=window.print () >
</form>

12. Move the window


<form name=form1>
Horizontal direction <input Type=text name=x value=20>
Vertical direction <input Type=text name=y value=50>
<input type=button value= "Move window to ..." Onclick=window.moveto (document.form1.x.value,document.form1.y.value) >
</form>

<form name=form1>
Horizontal direction <input Type=text name=x value=20>
Vertical direction <input Type=text name=y value=50>
<input type=button value= "Move Window" Onclick=window.moveby (document.form1.x.value,document.form1.y.value) >
</form>


13. Change the window size


<form name=form1>
Horizontal direction <input Type=text name=x value=200>
Vertical direction <input Type=text name=y value=500>
<input type=button value= "Change window size to ..." Onclick=window.resizeto (Document.form1.x.value,document.form1.y.value) >
</form>

<form name=form1>
Horizontal direction <input Type=text name=x value=200>
Vertical direction <input Type=text name=y value=500>
<input type=button value= "Change window Size" Onclick=window.resizeby (document.form1.x.value,document.form1.y.value) >
</form>

14. Notify the user with a warning dialog box

<script>
Window.alert ("Welcome")
</script>

15. Use the prompt dialog box to accept input

<script>
Name=window.prompt ("Enter name", "name")
document.write ("Welcome:" +name+ "come Here")
</script>

16. Use the confirmation dialog box to make a decision for the user

<script>
Like=window.confirm ("Do you think it's OK?")
if (like==true)
document.write ("Thank you for your compliment")
Else
document.write ("Hope to get your compliments")
</script>

Chapter Nineth Using Strings

1. Using String objects

<script>
Mystring= "Gdgdfgfddddaaaaaaaaaaaabbbbbbbbbbbbbbbbbvbhg.<br>"
document.write (mystring)
document.write (Mystring.bold ())
document.write (Mystring.touppercase ())
</script>

2. Using substrings

<script>
str1= "FDSF 1111 gfdgfd dfdsf CCCC"
document.write (STR1)
document.write (str1.substring (0,13) + "<br>")
document.write (Str1.substr (20,11) + "<br>")
</script>

3. Connection string

<script>
Str1= "May and find"
Str2= "Peace,happiness and Prosperity.<br>"
document.write (str1+ "<br>")
document.write (STR2)
document.write (Str1.concat (STR2))
document.write (STR1+=STR2)
</script>

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 Anchors and links

<script>
Str1= "This is the bigginning of the page.<br>"
Str2= "....<br>"
Str3= "This is 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 string length

<script>
Str1= "This is the bigginning of the page."
document.write (str1+ "<br>")
document.write ("The length of the string is:" +str1.length)
document.write ("string all uppercase is;" +str1.touppercase ())
document.write ("string all lowercase is;" +str1.tolowercase ())
</script>

7. Search within a string

<script>
Str1= "This are the end of the line.<br>"
document.write (STR1)
document.write ("character end" in the position of the string is "+str1.search" ("End"))
document.write ("character dog at the position of the string is" +str1.search ("Dog"))
</script>

8. Locating characters in a string

<script>
Str1= "Spring is a" for flowers and trees and baby bunnles<br> "
document.write (STR1)
document.write ("The index for the second word ' and ' are" +str1.indexof ("and", 30))
Documednt.write ("The last index of the word ' and" +str1.lastindexof ("and"))
</script>

9. Replace text in a string

<script>
Str1= "Spring is a" 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" 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 tenth use date and time

1. Use Date Object

<script>
Cdate=new Date ("August 2,1989 12:30:00")
document.write (CDate)
</script>

2. Show local time and date

<script>
Cdate=new Date ()
document.write ("Current time is:" +cdate.togmtstring () + "<br>")
document.write ("Date and Time is:" +cdate.tolocalestring ())
</script>

3. Get time and date values

<script>
Cdate=new Date ()
document.write ("Show Current Week" +cdate.getday () + "<br>")
document.write ("Show Current Month" +cdate.getmonth () + "<br>")
document.write ("Show Current Date" +cdate.getday () + "<br>")
document.write ("Show Current Year" +cdate.getyear () + "<br>")
document.write ("Show Current Hour" +cdate.gethours () + "<br>")
document.write ("Show Current Minutes" +cdate.getminutes () + "<br>")
document.write ("Show Current Seconds" +cdate.getseconds () + "<br>")
</script>

4. Set time and date values

<script language=javascript>
Cdate=new Date ("December 25,1984")
document.write ("Show Date" +cdate+ "<br>")
document.write ("Set month" +cdate.setmonth + "<br>")
document.write ("Set Date" +cdate.setdate + "<br>")
document.write ("Set Year" +cdate.setyear + "<br>")
document.write ("Set hour" +cdate.sethours + "<br>");
document.write ("Set Minutes" +cdate.setminutes () + "<br>");
document.write ("Set seconds" +cdate.setseconds + "<br>");
document.write ("Show date and time after setting" +cdate);
</script>

Chapter 11th using the Math object

1. Using the Math object

<script language=javascript>
</script>
<form name=form1>
Radius of the circle: <input type=text name=rad><br>
Area of the circle: <input type=text name=area><br>
<input Type=button name=button1 value= Compute the area of the circle onclick=document.form1.area.value=document.form1.rad.value* Document.
Form1.rad.value*math.pi>
</form>

2. Generate Random Numbers

<script>
Array1=new Array (
"This is the 1th sentence",
"This is the 2nd sentence",
"This is the 3rd sentence",
"This is the 4th sentence",
"This is the 5th 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 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= Compute square root
>
</form>

4. Rounding of Numbers

<form name=form1>
Enter <input Type=text name=val><br>
Rounding Results <input Type=text name=round><br>
<input Type=button name=button1 value= calculated result Onclick=document.form1.round.value=math.round ( Document.form1.val.value) >
</form>

5. exponentiation operation

<form name=form1>
Base <input Type=text name=val><br>
Index <input Type=text name=power><br>
Power <input Type=text name=result><br>
<input Type=button name=button1 value= Calculation results >
</form>

6. Find minimum and maximum values

<form name=form1>
Digital 1<input Type=text name=val1><br>
Digital 2<input Type=text name=val2><br>
Minimum value <input type=text name=min><br>
Maximum Value <input type=text name=max><br>
Digital 1<input Type=button value= calculation >
</form>

Chapter 12th using Forms

1. Use a text box


<form name=form1>
<input type=text value= "information, please" name=text1>
</form>
<script>
document.write (Form Text1 type is: "+document.form1.text1.type+" <br>)
document.write ("Form Text1 name is:" +document.form1.text1.name+ "<br>")
document.write ("Form Text1 value is:" +document.form1.text1.value+ "<br>")
document.write ("Form Text1 size 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 Password box

<form name=form1>
<input Type=password NAME=PW1 value=daylight>
</form>
<script>
document.write (PW1 type of form: +document.form1.pw1.type+ "<br>")
document.write (pw1 name of form: "+document.form1.pw1.name+" <br>)
document.write ("Pw1 Value of form:" +document.form1.pw1.value+ "<br>")
document.write (pw1 size of form: "+document.form1.pw1.size+" <br>)
</script>

3. Using hidden fields

<form name=form1>
<input Type=hidden name=hid1 value=piece of eight>
</form>
<script>
document.write (hid1 type of form: +document.form1.hid1.type+ "<br>")
document.write (hid1 name of form: "+document.form1.hid1.name+" <br>)
document.write ("Hid1 Value of form:" +document.form1.hid1.value+ "<br>")
</script>

4. Use text area box


<form name=form1>
<textarea name=ta1>how Many grains of sand are there in the Sahara Desert?</textarea>
</form>
<script>
document.write (ta1 type of form: +document.form1.ta1.type+ "<br>")
document.write (ta1 name of form: "+document.form1.ta1.name+" <br>)
document.write ("Ta1 Value of form:" +document.form1.ta1.value+ "<br>")
document.write (horizontal width of form ta1: "+document.form1.ta1.cols+" <br>)
document.write (ta1 vertical width of the form: "+document.form1.rows.value+" <br>)
</script>

6. Use the reset button

<form name=form1>
<input type=reset name=reset1 value= "Rest form" >
</form>
<script>
document.write (reset1 type of form: +document.form1.reset1.type+ "<br>")
document.write (reset1 name of form: "+document.form1.reset1.name+" <br>)
document.write ("Reset1 Value of form:" +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 (submit1 type of form: +document.form1.submit1.type+ "<br>")
document.write (submit1 name of form: "+document.form1.submit1.name+" <br>)
document.write ("Submit1 Value of form:" +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 (CB1 type of form: +document.form1.cb1.type+ "<br>")
document.write ("Form CB1 is selected?:" +document.form1.cb1.checked+ "<br>")
document.write (CB1 name of form: "+document.form1.cb1.name+" <br>)
</script>

9. Use radio buttons

<form name=form1>
<input Type=radio Name=radio1>male
<input Type=radio Name=radio1>female
</form>
<script>
document.write ("First button selected" +document.form1.radio1[0].checked+ "<br>")
document.write ("second button selected" +document.form1.radio1[1].checked+ "<br>")
document.write ("button name" + document.form1.radio1[0].name+ "<br>")
document.write ("Number of buttons" +document.form1.radio1.length)
</script>

10. Use the SELECT 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 ("The name of this select list" +document.form1.select1.name+ "<br>")
document.write ("The length of this select list" +document.form1.select1.length+ "<br>")
document.write ("This select list is currently selected index number" +document.form1.select1.selectedindex+ "<br>")
document.write ("The size of this select 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 ("Please enter a 1~4 integer")
}
}
</script>
<form name=form1>
Please enter a 1~4 integer:
<input type=text name=text1 size=4 onchange=validate () >
</form>

12. Control the form focus

<form name=form1>
<input Type=text name=text1 Value=where is for 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 the columns

The 14th chapter uses navigator

1. Using the Navigator object

<script>
document.write ("Navigator object's Properties" + "<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 ("Navigator object Method" + "<br>")
document.write ("javaenabled ():" +navigator.javaenabled ())
</script>

2. Check the user's browser

<script>
if (Navigator.appName.indexOf ("Microsoft")!=-1) {
document.write ("User browser is Microsoft's IE browser" + "<br>")}
else if (navigator.appName.indexOf ("Netscape")!=-1) {
document.write ("User browser is Netscape's Netscape browser" + "<br>")}
if (Navigator.appVersion.indexOf ("4.0")!=-1) {
document.write ("You are using a version 4.0compatible browser")
}
else{
document.write ("This browser are not 4.0 compliant")}
</script>

3. Detect the user's operating system

<script>
if (Navigator.platform.indexOf ("Win32")!=-1) {
document.write ("You are using a computer running Windows or Highter")}
else{
document.write ("This computer are not running Windows or higher")}
</script>

4. Using the Location object


<script>
document.write ("Location object's Properties" + "<br>")
document.write ("hash" +location.hash+ "<br>")
document.write ("hostname" +location.hostname+ "<br>")
document.write ("host" +location.host+ "<br>")
document.write ("href" +location.href+ "<br>")
document.write ("Port" +location.port+ "<br>")
document.write ("Search" +location.search+ "<br>")
</script>

Reload Web page
<form name=form1>
<input Type=button name=button1 value= Reload this page onclick=location.reload>
</form>


5. Use cookies


<script>
Finction Makecookie () {
if (!document.cookie) {
Name=prompt ("Please enter your name");
Document.cookie= "Name=" +name+ ";";}
}
</script>

<body Onload=makecookie () >
<script>
function Makecookie () {
if (!document.cookie) {
Name=prompt ("Please 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.