JS Common functions

Source: Internet
Author: User
Tags floor function local time mathematical functions natural logarithm square root tagname 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 of 1/2), math.sqrt2 (square root value 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.

Event Source Object
Event.srcElement.tagName
Event.srcElement.type

Capture Release
Event.srcElement.setCapture ();
Event.srcElement.releaseCapture ();

Event Keys
Event.keycode
Event.shiftkey
Event.altkey
Event.ctrlkey

Event return value
Event.returnvalue

Mouse Position
Event.x
Event.y

form activity Elements
Document.activeelement

Binding Events
Document.captureevents (Event.keydown);

Accessing form elements
document.all ("TXT"). focus ();
document.all ("TXT"). Select ();

form Commands
Document.execcommand

form Cookies
Document.cookie

Menu Events
Document.oncontextmenu

Creating Elements
Document.createelement ("SPAN");

get elements based on mouse
Document.elementfrompoint (EVENT.X,EVENT.Y). tagname== "TD
Document.elementfrompoint (EVENT.X,EVENT.Y). appendchild (MS)

form Picture
document.images[Index]

form Event Bindings
Document.onmousedown=scrollwindow;

elements
Document. form. elements[Index]

Object Binding Events
Document.all.xxx.detachEvent (' onclick ', a);

Number of plugins
Navigator.plugins

take variable type
typeof ($js _libpath) = = "Undefined"
drop-down box
drop-down box. options[Index]
drop-down box. Options.length

Find Objects
Document.getelementsbyname ("R1");
document.getElementById (ID);

timed
Timer=setinterval (' ScrollWindow () ', delay);
Clearinterval (timer);

Uncode Encoding
Escape (), unescape

Parent Object

Obj.parentelement (DHTML)
Obj.parentnode (DOM)

the row of the interchange table
Tableid.moverow (2,1)//swap the third line with the second line

Replace CSS
Document.all.csss.href = "A.css";

Display Side -by
Display:inline

Hide Focus
Hidefocus=true

Line wrapping based on width

Style= "Word-break:break-all"

Auto Refresh
<meta http-equiv= "Refresh" content= "8; Url=http://hi.baidu.com ">

Simple Mail
<a href= "Mailto:[email protected]?subject=thisistitle&body=thisisthewordswhatyouwanttowriteforme" >

Quick go to location

Obj.scrollintoview (True)

Anchor

<a name= "First" >
<a href= "#first" >anchors</a>

Web page Pass Parameters

Location.search ();

can be edited

Obj.contenteditable=true

Execute menu Command

Obj.execcommand

Double-byte characters

/[^\x00-\xff]/

Kanji

/[\u4e00-\u9fa5]/

let the English string wrap beyond the table width
Word-wrap:break-word; Word-break:break-all;

Transparent Background

<iframe src= "1.htm" width=300 height=180 allowtransparency></iframe>

Get style content
Obj.style.cssText

HTML Tags
Document.documentElement.innerHTML

First style label
Document.stylesheets[0]

the first style in a style tag
Document.stylesheets[0].rules[0]

when you prevent clicking on an empty link, the page is often reset to the top end of the page.
<a href= "javascript:function ()" >word</a>

Previous page Source

Asp:
Request.ServerVariables ("Http_referer")
Javascript:
Document.referrer

Freeing Memory
CollectGarbage ();

Disable Right-click

Document.oncontextmenu = function () {return false;}

Prohibit save
<noscript><iframe src= "*.htm" ></iframe></noscript>

Prohibit selection
<body oncontextmenu= "return false" ondragstart= "return false" Onselectstart = "return false" onselect= " Document.selection.empty () "oncopy=" Document.selection.empty () "onbeforecopy=" return false "onmouseup=" Document.selection.empty () >

Prohibit Paste

<input Type=text onpaste= "return false" >

Address bar icon
<link rel= "Shortcut Icon" href= "Favicon.ico" >
Favicon.ico name Best not change 16*16 16 colors, put the virtual directory under the root directory

Favorites Bar Icons

<link rel= "Bookmark" href= "Favicon.ico" >

View Source

<input Type=button value= View page source code onclick= "window.location = ' view-source: ' + ' http://www.csdn.net/'" >

Turn off IME
<input style= "ime-mode:disabled" >

Auto Select All
<input type=text name=text1 value= "123" onfocus= "This.select ()" >

enter key to move the cursor to the next input box
<input onkeydown= "if (event.keycode==13) event.keycode=9" >

default values for text boxes
<input type=text value= "123" onfocus= "alert (this.defaultvalue)" >

get the microsecond that time represents
var n1 = new Date ("2004-10-10". Replace (/-/g, "\")). GetTime ()

whether the window is closed
Win.closed

checkbox Flat
<input type=checkbox style= "Position:absolute; Clip:rect (5px 15px 15px 5px) "><br>

Get Selection
Document.selection.createRange (). Duplicate (). Text

Auto-complete function
<input type=text autocomplete=on> turn on this feature
<input type=text autocomplete=off> Turn off this feature

window Maximization
<body onload= "Window.resizeto (window.screen.width-4,window.screen.height-50); Window.moveTo ( -4,-4)" >

no Close button ie
window.open ("aa.htm", "Meizz", "fullscreen=7");

Unified encoding/decoding
Alert (decodeURIComponent (encodeURIComponent ("http://hello. com?as= hehe"))
encodeURIComponent to ":", "/", ";" and "?" also coded

table row Indication
<tr onmouseover= "this.bgcolor= ' #f0f0f0 '" onmouseout= "this.bgcolor= ' #ffffff '" >

various sizes
s + = "\ r \ n page Visible area width:" + document.body.clientWidth;
s + = "\ r \ n page visible Area High:" + document.body.clientHeight;
s + = "\ r \ n page visible Area High:" + Document.body.offsetWeight + "(including edge width)";
s + = "\ r \ n page visible Area High:" + Document.body.offsetHeight + "(including edge width)";
s + = "\ r \ n Full text width:" + document.body.scrollWidth;
s + = "\ r \ n Full text High:" + document.body.scrollHeight;
s + = "\ r \ n page is rolled High:" + document.body.scrollTop;
s + = "\ r \ n page is rolled away left:" + document.body.scrollLeft;
s + = "\ r \ n Web page body part:" + window.screentop;
s + = "\ r \ n Page body part left:" + window.screenleft;
s + = "\ r \ n Screen resolution High:" + window.screen.height;
s + = "\ r \ n Screen resolution width:" + window.screen.width;
s + = "\ r \ n screen available work area height:" + window.screen.availHeight;
s + = "\ r \ n screen available work area width:" + window.screen.availWidth;

JS Common functions

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.