`

My JS

阅读更多

在很多项目中,我们都会在页面使用一些开源的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;
}
})();

 以上只是简要的写法并不美观,希望以后可以不断完善。

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics