"JavaScript" javascript common functions Encyclopedia

Source: Internet
Author: User
Tags floor function local time mathematical functions natural logarithm square root javascript array

JavaScript functions can be divided into five categories:
• General functions
• Array functions
• Date function
• Mathematical Functions
• String Functions
1. General functions
JavaScript general functions include the following 9 functions:
(1) Alert function: Displays a warning dialog box, including an OK button.
(2) Confirm function: Displays a confirmation dialog box, including OK, Cancel button.
(3) Escape function: Converts a character to a Unicode code.
(4) Eval function: Evaluates the result of an expression.
(5) isNaN function: The test is (true) No (false) is not a number.
(6) parsefloat function: Converts a string into a numeric form of a character.
(7) parseint function: Converts a string into an integer number form (which can be specified as a binary).
(8) Prompt function: Displays an input dialog box prompting for user input. For example:
<script language= "JavaScript" >
<!--
Alert ("Input error");
Prompt ("Please enter your name", "name");
Confirm ("OK no! ");
-
Script>
(9) Unescape function: Decodes characters encoded by the escape function.
2. Array functions
The JavaScript array function consists of the following 4 functions:
(1) Join function: Converts and joins all elements in an array to a string. Cases:
function Joindemo ()
{
var a, B;
A = new Array (0,1,2,3,4);
b = A.join ("-");//delimiter
return (b);//Returns the b== "0-1-2-3-4"
}
(2) Length function: Returns the lengths of the array. Cases:
function Lengthdemo ()
{
var a, l;
A = new Array (0,1,2,3,4);
L = a.length;
return (l);//l==5
}
(3) Reverse function: Reverses the order of the array elements. Cases:
function Reversedemo ()
{
var a, l;
A = new Array (0,1,2,3,4);
L = A.reverse ();
return (L);
}
(4) Sort function: Reorder the array elements. Cases:
function Sortdemo ()
{
var a, l;
A = new Array ("X", "Y", "D", "Z", "V", "M", "R");
L = A.sort ();
return (L);
}
3. Date function
The JavaScript date function consists of the following 20 functions:
(1) getdate function: Returns the day portion of the date, with a value of 1~31. Cases:
function Datedemo ()
{
var d, s = "Today ' s date is:";
D = new Date ();
s + = (D.getmonth () + 1) + "/";
s + = D.getdate () + "/";
s + = D.getyear ();
return (s);
}
(2) Getday function: Returns the day of the week with a value of 0~6, where 0 represents Sunday, 1 means Monday, ..., and 6 means Saturday. Cases:
function Datedemo ()
{
var d, Day, X, s = "Today is:";
var x = new Array ("Sunday", "Monday", "Tuesday");
var x = X.concat ("Wednesday", "Thursday", "Friday");
var x = x.concat ("Saturday");
D = new Date ();
Day = D.getday ();
return (s + = X[day]);
}
(3) Gethouse function: Returns the "hour" portion of the date, with a value of 0~23. Cases.
function Timedemo ()
{
var d, s = "The current local time is:";
var c = ":";
D = new Date ();
s + = d.gethours () + C;
s + = D.getminutes () + C;
s + = D.getseconds () + C;
s + = D.getmilliseconds ();
return (s);
}
(4) Getminutes function: Returns the "minute" part of the date with a value of 0~59. See the example above.
(5) GetMonth function: Returns the month portion of the date, with a value of 0~11. 0 means January, 2 means March, ..., and 11 means December. See the previous example.
(6) Getseconds function: Returns the "second" part of the date with a value of 0~59. See the previous example.
(7) GetTime function: Returns the system time.
function Gettimetest ()
{
var d, S, t;
var Minmilli = 1000 * 60;
var Hrmilli = Minmilli * 60;
var Dymilli = Hrmilli * 24;
D = new Date ();
t = D.gettime ();
s = "It ' s Been"
s + = Math.Round (T/dymilli) + "days since 1/1/70";
return (s);
}
(8) getTimezoneOffset function: Returns the difference in this area (local time and GMT Greenwich Mean Time), in minutes.
function Tzdemo ()
{
var d, tz, S = "The current local time is";
D = new Date ();
TZ = D.gettimezoneoffset ();
if (TZ < 0)
s + = tz/60 + "hours before GMT";
else if (TZ = = 0)
s + = "GMT";
Else
s + = tz/60 + "hours after GMT";
return (s);
}
(9) GetYear function: Returns the "year" part of the date. The return value is based on a 1900-year base, for example, 1999 is 99. There is an example above.
Parse function: Returns the number of milliseconds (local time) that was counted from zero January 1, 1970.
function Gettimetest (testdate)
{
var d, S, t;
var Minmilli = 1000 * 60;
var Hrmilli = Minmilli * 60;
var Dymilli = Hrmilli * 24;
D = new Date ();
t = Date.parse (testdate);
s = "There is"
s + = Math.Round (Math.Abs (T/dymilli)) + "Days"
s + = "between" + TestDate + "and 1/1/70";
return (s);
}
(one) setdate function: The "day" portion of the set date, with a value of 0~31.
sethours function: The "hours" section of the set date, with a value of 0~23.
setminutes function: The "Minutes" section of the set date, with a value of 0~59.
Setmonth function: Sets the month portion of the date, and the value is 0~11. 0 of them represent January, ..., 11 means December.
() setseconds function: The "Seconds" section of the set date, and the value is 0~59.
() settime function: Set the time. The time value is the number of milliseconds since January 1, 1970 when the integer count is zero.
() setyear function: The "Year" section of the set date.
togmtstring function: The conversion date becomes a string, GMT Greenwich Standard Time.
() setlocalestring function: Converts the date into a string, for local time.
(20) UTC function: Returns the number of milliseconds from January 1, 1970 to the full count, measured in GMT Greenwich Mean time.
4. Mathematical Functions
The JavaScript math function is actually the math object, which includes attributes and functions (or methods) in two parts. Where the attribute mainly has the following content.
Math.e:e (natural logarithm), MATH.LN2 (natural logarithm of 2), MATH.LN10 (natural logarithm of 10), math.log2e (logarithm of E, base 2), math.log10e (logarithm of E, base 10), Math.PI (π), Math.sqrt1_2 (square root value of 1/2), Math.sqrt2 (square root of 2).
The function has the following 18:
(1) ABS function: Math.Abs (the same as below), returns the absolute value of a number.
(2) ACOs function: Returns the inverse cosine of a number with a result of 0~π radians (radians).
(3) ASIN function: Returns the inverse chord value of a number, resulting in-Π/2~Π/2 radians.
(4) Atan function: Returns the inverse tangent of a number, resulting in-Π/2~Π/2 radians.
(5) ATAN2 function: Returns the polar coordinate angle value of a coordinate.
(6) Ceil function: Returns the smallest integer value (greater than or equal to) of a number.
(7) Cos function: Returns the cosine of a number with the result of -1~1.
(8) Exp function: Returns the value of the exponent of E (natural logarithm).
(9) Floor function: Returns the maximum integer value (less than or equal to) of a number.
Log function: A natural logarithm function that returns the natural logarithm (e) value of a number.
(one) Max function: Returns the maximum value of two digits.
Min function: Returns the minimum value of two digits.
Pow function: Returns the value of a number.
Random function: Returns a 0~1 value.
Round function: Returns the rounded value of a number with an integer type.
Sin function: Returns the sine of a number with the result of -1~1.
sqrt function: Returns the square root value of a number.
Tan function: Returns the tangent of a number.
5. String functions
The JavaScript String functions complete the following 20 functions for font size, color, length, and lookups of strings:
(1) Anchor function: Generates a link point (anchor) for use as a hyperlink. The Anchor function sets the name of the link, and another function link sets the URL address.
(2) Big function: Add the font to a number, and ... The label results are the same.
(3) Blink function: Make the string blink, with ... The label results are the same.
(4) Bold function: Make the font bold, with ... The label results are the same.
(5) Charat function: Returns a character specified in the string.
(6) Fixed function: Set the font to a fixed-width font, with ... The label results are the same.
(7) FontColor function: Sets the font color, the same as the label result.
(8) FontSize function: Sets the font size, the same as the label result.
(9) IndexOf function: Returns the index of the first lookup in a string, starting from the left.
(Ten) Italics function: Make the font italic, with ... The label results are the same.
(one) LastIndexOf function: Returns the index of the first lookup in a string, starting from the right.
Length function: Returns the lengths of the strings. (without parentheses)
Link function: Generates a hyperlink equivalent to the Set URL address.
Small function: Reduce the font size by one and ... The label results are the same.
Strike function: Add a horizontal line to the middle of the text, with ... The label results are the same.
() Sub function: The display string is the subscript (subscript).
SUBSTRING function: Returns the number of characters specified in the string.
SUP function: The display string is the superscript word (superscript).
() toLowerCase function: Converts a string to lowercase.
() toUpperCase function: Converts a string to uppercase.


Identification can be put into head>. Or... Between. Place the JavaScript identity ... Between the head, which is loaded before the home page and the rest of the code, which makes the code more powerful; Place the JavaScript identity in ... Create documents dynamically between principals to implement certain parts.
By means of identification: If you do not know the JavaScript code of the browser, then all the identities therein are ignored, and if so, the result is executed. Using annotations This is a good programming habit that allows others to read your language.
1. Alert () is a JavaScript window object method whose function is to eject a string that has an OK dialog box and displays ().
Like what:
2. Document. Write () is the output function of a Document object whose function is to output a character or variable value in parentheses to a window; Close () is to close the output.
Like what:
3. Window.external.AddFavorite (', ' want Network Technology station ') tips to add websites to Favorites

Onload=favorate () to be added to the label, such as:
※ If you want to add a bookmark when you leave the page, simply
Onunload=favorate () can be changed to Onload=favorate ()
4. Close ()//close the window, if the window is not opened with a script, the confirmation dialog box will pop up.
such as: Exit the system or
Exit system
Get in touch with me
5. Confirm ()
Popup OK/Cancel dialog box
6. Function Document.oncontextmenu ()//Disable right mouse button;
7. Function Document.onselectstart ()//disable selection
8. Window.prompt () is a method of a Window object, and its basic function is to display a dialog box with "OK" and "Cancel" on the screen when loading a Web page, allowing you to output data.

The alert () method can create a dialog box with an OK button;
The Confirm () method provides a dialog box with two buttons for the programmer;
The prompt () method allows the user to enter information in a dialog box and can use the default value, which has the following basic format:
Prompt ("hint message", default value)
9. Window open () opens a new window.
The basic format is: window. Open ("URL", "Window name", "Window Properties")
The Window property parameter is a string list item that is separated by a comma, which indicates the properties of the newly created window.
Parameter SetPoint value meaning
Toolbar yes/no establishing or not establishing a standard tool bar
Location yes/no establishing or not establishing a position entry field
Directions yes/no Setting up or not establishing a standard Catalog button
Status yes/no establishing or not establishing a State bar
MenuBar yes/no Create or not create a menu bar
ScrollBar yes/no Create or not create scroll bars
Revisable yes/no can change window size
Width yes/no to determine window widths
Height yes/no determines how high the window is.
10. The output of the information data is displayed. The Window.alert (), document.write and Document.writln () methods are more commonly used.
Both the Write () and Writeln () methods are used to output text strings to the browser window; the only difference is that the Writeln () method automatically adds a carriage return after the text.
The alert () method is a method of the Window object, so when used, it is not necessary to write the window object name, but to use it directly. Its main purpose is to use the output to generate warning messages or prompt the user, once the user presses the "OK" button, you can continue to execute other scripts.
document.write (document.lastmodified) can produce the last modified date.

JavaScript itself is case-sensitive, lastmodified and LastModified, which in its view are different results. The document.lastmodified parameter was written documeut.lastmodified when Netscape 2.0 Beta 2, however, the later version was changed to Document.lastmodified. So pay attention to capitalization when writing.
12. Adding the onmouseover method to the traditional label can achieve a certain effect.
Such as:

Window.status is used to allow you to display some messages on the status column of the WWW browser. In the syntax, you can see that the message part is "enclosed", not "enclosed", and must be added after the end of the message section; return true.
You can use the onMouseOver method to call a function when an event occurs.

Text link: JavaScript common functions Daquan

"JavaScript" javascript common functions Encyclopedia

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.