1. Meaning of the path symbol
Src= "/js/jquery.js", ". /"This slash is the meaning of the absolute path, which means the root directory of the Web site.
Other such as "./", ". /"," Jquery.js "," js/jquery.js "and so on are relative paths to the current Web page.
2. Get the root directory of the Web site
function Getrootpath () {
var strfullpath = Window.document.location.href;
var strpath = Window.document.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 the parameters of the URL
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 ("a"));
4. Functions in JS
4.1 Math.Round four rounding five in
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.
document.write (Math.random ())
document.write (Math.floor (Math.random () *11)) Math object's Floor () method and random () to return a random number between 0 and 10
4.3 IsNaN () is a non-numeric, false if non-numeric true
4.4 Number () converts the value of an object to a digit
4.5 parsefloat () parseint () returns NaN if the first character of a string cannot be converted to a number
The 4.6 string () function converts the value of an object to a string
5. Arrays
5.1 Number combination concat array, generate new array, the original array is unchanged
var arr = new Array (3)//define 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))
A combination of 5.2 numbers is merged into a string join. The default is "," connected, which can be specified, such as join (".")
6. The regular expression most commonly used is test (), found to be true, otherwise false
var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life is free");
7. Events
7.1 OnLoad and OnUnload page load, unload time call
7.2 onfocus, OnBlur, and OnChange events often work with each other to validate forms
<input type= "text" size= "id=" email "onchange=" checkemail () ">
7.3 OnSubmit is used to validate all form fields before submitting a form
/*
Here is an example of using the OnSubmit event. The Checkform () function is called when the user clicks on the confirmation button in the form. If the value of the field is not valid, the 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, conversely, the commit is canceled. */
<form method= "POST" action= "xxx.htm" onsubmit= "return Checkform ()" >
8. Cookies
8.1 Create
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 ())
}
8.2 Read
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", milliseconds) cleartimeout (t)//Stop timing
10. Open Web Site
10.1 Open Web site in another window window.open ()
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 Web site in the same window
window.location.href = ' http://www.sohu.com ';
11. Objects
11.1 Object definition, destroying
var oobject = new Object;
Do something with the object here
Oobject = null;
11.2 Defining classes
function Cat (name,color) {
THIS.name = name;
This.color = color;
This.type = "Cat animal";
This.eat = function () {alert ("Eat Mouse");
}
11.3 Using JSON to construct an object
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 Using prototype to construct an object
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);
}
2015.09.13