总结一些js自定义的函数
function dayin() { var code="<body onload=window.print()>" code+=document.all.dayin.innerHTML; code=code.toUpperCase(); code=code.replace(/<A[^>]*>删除<\/A>/gi, ""); code=code.toLowerCase(); var newwin=window.open(‘‘,‘‘,‘‘); newwin.opener = null; newwin.document.write(code); newwin.document.close(); } |
是过滤掉内容中的所有删除连接
function isNumber(st) { var Letters = "1234567890-."; var i; var c; if(st.charAt( 0 )==‘.‘) return false; if(st.charAt( 0 )==‘-‘&&st.charAt( 1 )==‘.‘) return false; if( st.charAt( st.length - 1 ) == ‘-‘ ) return false; for( i = 0; i < st.length; i ++ ) { c = st.charAt( i ); if (Letters.indexOf( c ) < 0) return false; } return true; } |
function createCookie(name,value,days){ var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = "; expires="+date.toGMTString(); }; document.cookie = name+"="+value+expires+"; path=/"; }; |
function readCookie(name){ var nameEQ = name + "="; var ca = document.cookie.split(‘;‘); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==‘ ‘) c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); }; return null; }; |
function request(st) { var ustr=document.location.search; var intPos = ustr.indexOf("?"); var strRight = ustr.substr(intPos + 1); var arrTmp = strRight.split("%26"); for(var i = 0; i < arrTmp.length; i++) { var arrTemp = arrTmp[i].split("="); if(arrTemp[0].toUpperCase() == st.toUpperCase()) return arrTemp[1]; } return ""; } |
function hideObject(obj) { obj.style.display = "none"; } |
function showObject(obj) { obj.style.display = "block"; } |
function trim(str) { return str.replace(/^\s*|\s*$/g,""); } |
作用:比较d1,d2日期的大小
function bj_date(d1,d2) { /* author:wxg 作用:比较日期大小 参数:d1 d2 字符型 年-月-日 类型,如 2005-01-22 返回值: 0/1/2 数字型 d1>d2 返回0 d1=d2 返回1 d1<d2 返回2 */ if(d1==""&&d2==""){ return 3 } if(d1==""||d2==""){ return 4 } d1=d1.split("-") d2=d2.split("-") var a = new Date(Number(d1[0]),Number(d1[1]),Number(d1[2])) var b=new Date(Number(d2[0]),Number(d2[1]),Number(d2[2])) a = a.valueOf() b=b.valueOf() if(a-b>0) return 0 if(a-b==0) return 1 if(a-b<0) return 2 } |
if(/[^0-9\.\-]/.test(s)) return "invalid value";
s=s.replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
s=s.replace(".",",");
var re=/(\d)(\d{3},)/;
while(re.test(s))
s=s.replace(re,"$1,$2");
s=s.replace(/,(\d\d)$/,".$1");
return s.replace(/^\./,"0.")
}
var winname = window.open(‘‘, "_blank", ‘‘);
winname.document.open(‘text/html‘, ‘replace‘);
winname.opener = null // 防止代码对论谈页面修改
winname.document.writeln(obj.value);
winname.document.close();
}
var winname = window.open(‘‘, ‘_blank‘, ‘top=10000‘);
winname.document.open(‘text/html‘, ‘replace‘);
winname.document.writeln(obj.value);
winname.document.execCommand(‘saveas‘,‘‘,‘code.htm‘);
winname.close();
}
var count=0, asc, test=‘中文‘;
if (test.length==2) {
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
}
}
return str.length+count;
}
function strSubLeft(str, len) {
var count=0, asc, ret = "";
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
if (i + count >= len) {
ret = str.substr(0, i);
break;
}
}
return ret;
}
function getRandom(){
return "&"+Math.random()*1000+"&";
}
if(str==null||str==""){
//alert("请输入数字!");
return false;
}
var digits = "1234567890";
var i = 0;
var strlen = str.length;
while((i<strlen)){
var char = str.charAt(i);
if(digits.indexOf(char) == -1) {
//alert("请输入正确的数字!");
return false;
}
i++;
}
return true;
}
function hasWord(str){
if(str.replace(/ /g,‘‘)==""||str.replace(‘ ‘,‘‘)==""){
return false;
}else{
return true;
}
}
function replaceAll(oldStr,findStr,repStr){
var srchNdx = 0;
var newStr = "";
while (oldStr.indexOf(findStr,srchNdx) != -1){
newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
newStr += repStr;
srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
}
newStr += oldStr.substring(srchNdx,oldStr.length);
return newStr;
}
function checkPrice(inputValue){
inputValue=""+inputValue;
myRegExp = /^(\d+),(\d+)$/;
if (inputValue.match(myRegExp)) return true;
else return false;
}
function checkNum(valueStr){
var myRegExp = /^(\d+)$/;
if (valueStr.match(myRegExp)){
return true;
}else{
return false;
}
}
/*限制obj(通常为TEXTAREA)中的字符个数*/
function checkWordsLength(obj,maxLength){
if(obj.value.length>=maxLength){
obj.value="http://blogbeta.blueidea.com/obj.value.substring(0,maxLength-1);
" }
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。