在很多项目中,我们都会在页面使用一些开源的jsframe,比如ext、jquery、prototype等,有时候只用到其中很少的方法或特性,比如$。
这样在一个并不复杂的页面中需要到如一个framejs是十分不划算的,因此我写了这个common.js,希望能在某些时候减少页面加载时间。
这些JS通用方法,包括$和基于prototype字符串的验证方法:
// Dongao Common scripts
// version 1.1
// author greate nomandia
(function(){
if ( !Class ){
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
}
var DU = Class.create();
DU.prototype = {
initialize : function(){},
version : '1.0',
$ : {},
commonRegs : {
Require : /.+/,
Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,
Mobile : /^((\(\d{3}\))|(\d{3}\-))?1\d{10}$/,
Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
IdCard : /^\d{15}(\d{2}[\w\d])?$/,
Currency : /^\d+(\.\d+)?$/,
Number : /^\d+$/,
Zip : /^[1-9]\d{5}$/,
QQ : /^[1-9]\d{4,8}$/,
Integer : /^[-\+]?\d+$/,
Double : /^[-\+]?\d+(\.\d+)?$/,
English : /^[A-Za-z]+$/,
Chinese : /^[\u0391-\uFFE5]+$/,
UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
IsSafe : function(str){return !this.UnSafe.test(str);},
Passport: /[\u0391-\uFFE5\w\d_-]{6,20}/,
datetime :/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/
},
checkAllByName : function(name, obj){
var chks = $N(name);
if(!chks){ return false; }
var i=0,len=chks.length;
for (;i<len;i++ ){
if( chks[i].type=='checkbox' ){chks[i].checked=obj.checked;}
}
return obj.checked;
},
getCheckedCnt : function(name){
var chks = $N(name);
if(!chks) {return false;}
var i=0,len=chks.length,cnt=0;
for(;i<len;i++){
if( chks[i].type=='checkbox' && chks[i].checked){cnt++;}
}
return cnt;
},
round : function(number,X){
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
},
getDocumentBody : function(){
return document.body || document.documentElement;
},
disableMouseRightBtn : function(){
this.getDocumentBody().oncontextmenu = function(){ if( window.event ) window.event.returnValue = null; }
},
disableJSError : function(){
window.onerror = function(){return false;};
},
bindEvent : function(o, method, func){
if ( o.attachEvent ) {
o.attachEvent("on"+method,func);
} else if ( o.addEventListener ){
o.addEventListener(method,func,false);
} else {}
},
removeEvent : function(o, method, func){
if (o.removeEventListener) {
o.removeEventListener(method, func, false);
} else if (o.detachEvent) {
try {
o.detachEvent('on' + method, func);
} catch (e) {}
} else {}
},
open : function(url, width, height, winname){
var _winname=winname || 'DongaoOpenWin';
var _width = width || 500;
var _height= height || 300;
var _features='height='+_height+',width='+_width+',left='+((screen.width-_width)/2)+',top='+((screen.height-_height)/2)+',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no';
window.open(url, _winname, _features);
},
href : function(href, params){
var _params = params ? encodingURI(params) : '';
window.location.href = href+(''==_params?'':'?'+_params );
},
remove : function(obj){
var _obj = ( 'string' == typeof obj ) ? $(obj) : obj;
if( !_obj ) { return false; }
var _parent = _obj.parentNode;
if( _parent ) _parent.removeChild(_obj);
}
};
window.DU = new DU();
// add protype functions for all string object in scripts. include : trim, judgement of type, byte length get ... etc.
String.prototype.trim = function(){ return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); }
String.prototype.test = function(reg){ return window.DU.commonRegs[reg].test(this); } // return whether it matched with reg.
String.prototype.isNotNull = function(){return this.test('Require');}
String.prototype.isNull = function(){return !this.test('Require');}
String.prototype.inRange = function(min,max){return min<=this.dataLen()&&this.dataLen()<=max;}
String.prototype.lt = function(value){return (this.dataLen()<value);}
String.prototype.gt = function(value){return (this.dataLen()>value);}
String.prototype.isEmail = function(){return this.test('Email');}
String.prototype.isPhone = function(){return this.test('Phone');}
String.prototype.isMobile = function(){return this.test('Mobile');}
String.prototype.isUrl = function(){return this.test('Url');}
String.prototype.isIdCard = function(){return this.test('IdCard');}
String.prototype.isCurrency = function(){return this.test('Currency');}
String.prototype.isNumber = function(){return this.test('Number');}
String.prototype.isPostCode = function(){return this.test('Zip');}
String.prototype.isQQ = function(){return this.test('QQ');}
String.prototype.isInt = function(){return this.test('Integer');}
String.prototype.isDb = function(){return this.test('Double');}
String.prototype.isEnglish = function(){return this.test('English');}
String.prototype.isChinese = function(){return this.test('Chinese');}
String.prototype.isUnSafe = function(){return this.test('UnSafe');}
String.prototype.isIsSafe = function(){return this.test('IsSafe');}
String.prototype.isPassport = function(){return this.test('Passport');} // for user regist, include chinese,english,number,underline and minus-sign
String.prototype.bytesLen = function(){ if (''==this.length){return 0;}var i=0,len=this.length,cc=0,total=0;for(i=0;i<len;i++){cc=this.charCodeAt(i);total+=( 0<=cc&&cc<=255 ) ? 1 : 2;}return total;}
String.prototype.dataLen = function(){ return this.trim().bytesLen(); }
String.prototype.isDatetime = function(){ return this.test('datetime');}
window.$ = function(){
if ( !arguments){
if( document.all ){ return document.all; }
} else if ( arguments.length == 2 ){
if ( 'string' == typeof(arguments[1]) ){
return document.getElementById(arguments[0]).getElementsByTagName(arguments[1]);
} else if ( 'int' == typeof(arguments[1] )){
return document.getElementById(arguments[0]).childNodes[arguments[1]];
}
} else if ( arguments.length==1 ){
return document.getElementById(arguments[0]);
}
return null;
}
window.$N = function(){
if( !arguments ){
if( document.all ){ return document.all }
} else if( arguments.length > 0 && 'string' == typeof(arguments[0]) && document.getElementsByName ){
return document.getElementsByName(arguments[0]);
}
return null;
}
window.$Tag = function(){
if ( !arguments ){
if( document.all ){ return document.all }
} else if( arguments.length > 0 && 'string' == typeof(arguments[0]) && document.getElementsByTagName ){
return document.getElementsByTagName(arguments[0]);
}
return null;
}
})();
以上只是简要的写法并不美观,希望以后可以不断完善。
分享到:
相关推荐
一本介绍js的书,很不错,英文版 一本介绍js的书,很不错,英文版
js,日期插件,my97 在一个文本框内人性化输入日期!
My97DatePicker js日历控件
NULL 博文链接:https://heagle2006.iteye.com/blog/577950
my first js resources
My97DatePicker目录是一个整体,不可破坏里面的目录结构,也不可对里面的文件改名,可以改目录名 My97DatePicker.htm是必须文件,不可删除(4.8以后不存在此文件) 各目录及文件的用途: WdatePicker.js 配置文件,在调用的...
jQuery轮播图插件mySystem.js
My97DatePicker 时间插件 功能很强大
用JavaScript原生封装的一个jQuery库,可供大家学习底层封装原理。有addClass 添加类,removeClass 删除类,toggleClass 切换类,each 遍历函数,show() 显示, hide() 隐藏,toggle() 切换, html() 获取或设置html...
My97DatePicker目录是一个整体,不可破坏里面的目录结构,也不可对里面的文件改名,可以改目录名 My97DatePicker.htm是必须文件,不可删除(4.8以后不存在此文件) 各目录及文件的用途: WdatePicker.js 配置文件,在调用的...
My97DatePicker date 控件 ;" class="Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM'})" /> 可格式化
js日期控件--My97DatePicker
my97日历 JS日历 里面有测试文件
My97DatePicker,js做的时间日期输入控件
JS日历控件 4.8 Beta4版本 此版本为 4.8 Beta4 build 20131010
js 日历插件 My97 DatePicker 内附详细说明
更新内容: [新增]全键盘操作,不用鼠标也可以选择日期 [新增]dateFmt可以直接使用%y %M %d %ld等内置常量 [新增]isShowOK属性,可以隐藏确定按钮 [新增]当使用onfocus触发时,焦点在日期控件时,也会弹出日期控件 ...
My97DatePicker超实用的JS日期选择插件,不管是从外观功能上说,还是从易用性方面说,My97DatePicker都是全部占优的,也是比较有名的网页日期选择插件,支持多种调用方式,支持多种时间选择方式,甚至可以直接手动...
一套优秀的js日历控件My97 DatePicker 一款功能强大,体积小巧,速度一流,界面精美的JS日历控件,强烈推荐给广大网络编程人员!
my98(几种)日历控件的使用说明,实例演示,Js