JavaScript is often used resources

Common code

String interception

substr (start [, length]) 
Returns the specified length starting from a specified position of a substring 
substring (start, end) 
Returns a String object substring located at the specified location.

Jump pages

window.navigate("top.jsp");
window.history.back(-1);
window.location.href="login.jsp?backurl="+window.location.href; 
self.location='top.htm';
top.location='xx.jsp';

download finished

window.onload 
must wait for all the elements included in the page after the image is loaded to perform. 
Can not simultaneously write a plurality of execute only a 
$ (document) .ready () 
is finished after the drawing may be performed DOM structure 
can write a plurality of 
abbreviated $ ( function () {}); 
$ (window) .load () equivalent to window.onload

refresh page

history.go(0) 
location.reload() 
location=location 
location.assign(location) 
document.execCommand('Refresh') 
window.navigate(location) 
location.replace(location) 
document.URL=location.href

json parsing and transformation

JSON.parse("{a:'111',b:'ccc'}");  //解析
eval("("+""+")"); //解析

Time Conversion

var day1 = parseInt(new Date().valueOf()/1000); //获得当前时间时间戳
day2 = new Date(day1*1000);
alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds())
d = new Date();
s = d.getFullYear() + "-";
s += ("0"+(d.getMonth()+1)).slice(-2) + "-";
s += ("0"+d.getDate()).slice(-2) + " ";
s += ("0"+d.getHours()).slice(-2) + ":";
s += ("0"+d.getMinutes()).slice(-2) + ":";
s += ("0"+d.getSeconds()).slice(-2) + ".";
s += ("00"+d.getMilliseconds()).slice(-3);

URI encoding conversion

var A = " ':'" ; 
EN = the encodeURI (A);     // encoding 
A = decodeURI (EN);     // decode

HTML transcoding

function htmlEncode(value){
  return $('<div/>').text(value).html();
}

function htmlDecode(value){
  return $('<div/>').html(value).text();
}
 

Guess you like

Origin www.cnblogs.com/shareinfo/p/12169888.html