1. Math object
1.1 rounding and capturing
Ceil (x) rounded up, floor (x) rounded down, round (x) rounded down, parseint (x) truncated integer.
1.2 generate random number
Random () method implementation
Example:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> use the math. Random () method to generate a random number </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Body>
<Center> <font size = 4> Generate 0 ~ Random Number in the range of 100 </font> </center>
<Font size = "+ 1">
<Table border = "1" cellpadding = "2" cellspacing = "1">
<SCRIPT>
VaR n = 100;
For (I = 0; I <200; I ++)
{
If (I % 20 = 0)
{
Document. Write ("<tr> ");
}
Document. Write (math. Floor (math. Random () * (n + 1) + "<TD> ");
If (I + 1) % 20 = 0)
{
Document. Write (math. Floor (math. Random () * (n + 1 )));
}
}
</SCRIPT>
</Body>
</Html>
1.3 Other math methods
ABS (x) absolute value, X exponent power of exp (x) E, log (x) Base on 10 to take the logarithm, max (x, y) and min (x, y) take the greatest value. The y power of POW (x, y) X, and SQRT (X) is square.
2. Date of use
2.1 several forms of date object created
VaR now = new date; // default Structure
VaR mydate = new date ("month, DD, yyyy, HH: mm: SS"); // month, day, hour, minute, second
VaR mydate = new date ("month, DD, YYYY"); // month, day, and year
VaR mydate = new date (yyyy, mm, DD, HH, mm, SS); // year, month, day, hour, minute, second
VaR mydate = new date (yyyy, mm, DD); // year, month, and day
VaR mydate = new date (milliseconds); // The total number of milliseconds from, January 1, January 1, 1970 to the specified date
2.2 date setting method
Setdate (mm, DD) // you can specify a date for a month.
Setmonth (MM) // sets the month
Setfullyear (yyyy) // sets the year
Settime () // set the time (date), millisecond value counted from January 1, January 1, 1970
Sethours (), setminutes (), setseconds () // set the hour, minute, and second
2.3 date reading Method
Getdate (mm, DD) // read a certain date of a month
Getmonth (MM) // read the month
Getfullyear (yyyy) // read year
Gettime () // read time (date), millisecond value counted from January 1, January 1, 1970
Gethours (), getminutes (), getseconds () // read hour/minute/second
2.4 Date and Time Calculation
Example: calculate the number of days between two dates
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> calculate the number of days from today to the specified date </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Body>
<Font face = "Arial" size = 5 color = Red>
<SCRIPT>
VaR useryear = eval (prompt ("Enter the year (yyyy):", "2008 "));
VaR usermonth = eval (prompt ("Please input month (mm):", "8 "));
VaR userday = eval (prompt ("Enter the day (dd):", "1 "));
VaR future = new date (useryear, usermonth-1, userday); // calculate the millisecond difference between two dates
VaR today = new date ();
VaR diff = math. Abs (future. gettime ()-Today. gettime ());
VaR days = math. Floor (diff/(1000*60*60*24); // converts milliseconds to days.
Document. Write (today. tolocaledatestring () + "and" + future. tolocaledatestring () + ":" + days + "<br> ");
</SCRIPT>
</Body>
</Html>
3. Third-Party Library
Prototype is a javascript library that can simplify DOM object processing, form data processing, and remote Scripting (Ajax. by setting a prototype. you can use many improved functions of basic JavaScript by adding JavaScript files to your own documents.
Script. aculo. Us contains some functions that simplify drag-and-drop tasks. It also contains a number of combined effects to enhance the excessive sorghum animation effects on pages.
AJAX can implement communication between JavaScript and the lock running program on the Web server. this allows JavaScript to complete the work that could not have been completed, for example, dynamically loading information from the database without refreshing the page.
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> Local and new </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<SCRIPT>
Function changedata ()
{
VaR node = Document. getelementbyid ("mydiv ");
Node. firstchild. nodevalue = "updated data ";
}
</SCRIPT>
<Body>
<Div id = "mydiv"> original data </div>
<Input type = "button" value = "update data" onclick = "changedata ()">
</Body>
</Html>
4. Comprehensive instances
To begin.
Calculate the page stay time
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> Stay On page </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<SCRIPT>
VaR mydate = new date ();
Document. write ("current date:" + mydate. getyear () + "year" + mydate. getmonth () + "month" + mydate. getday () + "day ");
Function stop ()
{
VaR now = new date ();
Window. Alert ("stopped time" + (now. gettime ()-mydate. gettime ()/1000 + "second ");
}
</SCRIPT>
<Input type = button value = "Page browsing time" onclick = "Stop ()">
<Body>
</Body>
</Html>