JavaScript with half the effort (4)

Source: Internet
Author: User
Tags hash integer reset square root
Javascript

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>

<form name=form1>
<textarea name=ta1 rows=4 > How many grains a sand are there in the Sahara Desert?</textarea>
</form>

5. Use the button

<form name=form1>
<input Type=button name=button1 value= standard button >
</form>
<script>
document.write (button1 type of form: +document.form1.button1.type+ "<br>")
document.write (button1 name of form: "+document.form1.button1.name+" <br>)
document.write ("Button1 Value of form:" +document.form1.button1.value+ "<br>")
</script>

<form name=form1>
<input Type=text Name=text1 size=45><br>
<input Type=button name=button1 value=panic button >
</form>

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>



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.