Summary of common javascript Functions

Source: Internet
Author: User

1. Meaning of path symbols
Src = "/js/jquery. js", "../" is the absolute path, indicating the website root directory.
Others such ". /",".. /"," jquery. js "," js/jquery. js "and so on indicate the paths relative to the current web page and relative paths.
2. Get the root directory of the website
Copy codeThe Code is as follows:
Function GetRootPath (){
Var strFullPath = invalid argument Doc ument. location. href;
Var strPath = too many Doc ument. location. pathname;
Var pos = strFullPath. indexOf (strPath );
Var prePath = strFullPath. substring (0, pos );
Var postPath = strPath. substring (0, strPath. substr (1). indexOf ('/') + 1 );
Return (prePath + postPath );
}

3. Get url parameters
Copy codeThe Code is as follows:
// The url of the website such as: http://www.A.COM? A = 12
String. prototype. getQuery = function (name ){
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )");
Var r = this. substr (this. indexOf ("\? ") + 1). match (reg );
If (r! = Null) return unescape (r [2]); return null;
}
Var strHref = window. location. href;
Alert (strHref. getQuery (""));

4. Functions in js
4.1 Math. round
Copy codeThe Code is as follows:
Document. write (Math. round (0.60) + "<br/>") 1
Document. write (Math. round (0.50) + "<br/>") 1
Document. write (Math. round (0.49) + "<br/>") 0
Document. write (Math. round (-4.40) + "<br/>")-4
Document. write (Math. round (-4.60)-5

4.2 Math. random () returns a random number between 0 and 1.
Copy codeThe Code is as follows:
Document. write (Math. random ())
Document. write (Math. floor (Math. random () * 11) the floor () method and random () of the Math object return a random number between 0 and 10.

4.3 whether isNaN () is a non-numeric value. If it is a non-numeric value, true, otherwise false
4.4 Number () converts the object value to a Number
4.5 parseFloat () parseInt () if the first character of a string cannot be converted to a number, NaN is returned.
4.6 String () function converts the object value to a String
5. Array
The number of 5.1 is combined into an array concat combined arrays to generate a new array, the original array remains unchanged
Copy codeThe Code is as follows:
Var arr = new Array (3) // defines an Array
Arr [0] = "George"
Arr [1] = "John"
Arr [2] = "Thomas"
Var arr1 = new Array (3)
Arr1 [0] = "James"
Arr1 [1] = "Adrew"
Arr1 [2] = "Martin"
Var arr2 = arr. concat (arr1 ))

Join a string composed of 5.2 bytes. The default value is "," connected, which can be specified, such as join (".")
6. The most common regular expression is test (), which is true. Otherwise, it is false.
Copy codeThe Code is as follows:
Var patt1 = new RegExp ("e ");
Document. write (patt1.test ("The best things in life are free "));

7. Events
7.1 onload and onUnload page loading, called during uninstallation
7.2 onFocus, onBlur, and onChange events are usually used together to verify the form
<Input type = "text" size = "30" id = "email" onchange = "checkEmail ()">
7.3 onSubmit is used to verify all form fields before submitting a form
Copy codeThe Code is as follows:
/*
The following is an example of using the onSubmit event. When you click the OK button in the form, the checkForm () function is called. If the field value is invalid, this submission will be canceled. The Return Value of the checkForm () function is true or false. If the return value is true, the form is submitted, and vice versa. */
<Form method = "post" action = "xxx.htm" onsubmit = "return checkForm ()">

8. cookie
8.1 create
Copy codeThe Code is as follows:
Function setCookie (c_name, value, expiredays)
{
Var exdate = new Date ()
Exdate. setDate (exdate. getDate () + expiredays)
Document. cookie = c_name + "=" + escape (value) +
(Expiredays = null )? "": "; Expires =" + exdate. toGMTString ())
}

Read 8.2
Copy codeThe Code is as follows:
Function getCookie (c_name)
{
If (document. cookie. length> 0)
{
C_start = document. cookie. indexOf (c_name + "= ")
If (c_start! =-1)
{
C_start = c_start + c_name.length + 1
C_end = document. cookie. indexOf (";", c_start)
If (c_end =-1) c_end = document. cookie. length
Return unescape (document. cookie. substring (c_start, c_end ))
}
}
Return ""
}

9. Timing
SetTimeout () start timing
Var t = setTimeout ("javascript statement", millisecond) clearTimeout (t) // stop timing
10. Open the website
10.1 open website window. open () in another window ()
Copy codeThe Code is as follows:
Function openW (v ){
Var str = 'width = 200, height = 200, left = 200, top = 200, status = no, scrollbars = no ,'
Str + = 'menubar = no, toolbar = no, resizable = no, location = no'
Window. open (v, '', str );
}

10.2 open a website in the same window
Window. location. href = 'HTTP: // www.sohu.com ';
11. Object
11.1 define and destroy an object
Copy codeThe Code is as follows:
Var oObject = new Object;
// Do something with the object here
OObject = null;

11.2 definition class
Copy codeThe Code is as follows:
Function Cat (name, color ){
This. name = name;
This. color = color;
This. type = "cat ";
This. eat = function () {alert ("eat mouse ");};
}

11.3 construct an object using JSON
Copy codeThe Code is as follows:
Var People = {
Create: function (name, age ){
This. name = name;
This. age = age;
},
SayHello: function (){
Alert ("Hello, My name is" + this. name + ". I am" + this. age );
}
};

11.4 construct an object using prototype
Copy codeThe Code is as follows:
Var Person = function (name, age ){
This. name = name;
This. age = age;
};
Person. prototype. Introduce = function (){
Alert ("My name is" + this. name + ". I 'm" + this. age );
}

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.