javascript相关总结
2011-03-04 11:03
86 查看
在项目的开发过程中,我们总是能看到一些优秀的代码
但是很多时候都是没有留意去查看、学习、总结
1、javascript HashList数据结构的实现
2、ext_js的扩展实例
这是一个界面调试框的例子,
通过监听ctrl+shift+d来弹出相应的窗口,在窗口中可以输入相关js代码,
运行就能得到相关结果
3、其他相关处理的总结
这部分代码,以后再慢慢完善
相关字段数据验证处理
但是很多时候都是没有留意去查看、学习、总结
1、javascript HashList数据结构的实现
/** * HashList */ function HashList(){ this._data=new Array(); this._obj=new Object(); }; HashList.prototype.put=function(id,object){ if(id==null)return; if(typeof(id)=="object") id=id._hashCode; var data=this._data; var obj=this._obj; var idObj=obj[id]; if(typeof(idObj)=="undefined"){data.push(object);} else{ var _index=data.indexOf(idObj); if(_index>=0){data[_index]=object;} }; obj[id]=object; object._id=id; }; HashList.prototype.get=function(id){ if(typeof(id)=="number"){ return this._data[id]; } else{ if(typeof(id)=="object")id=id._hashCode;return this._obj[id]; } }; HashList.prototype.indexOf=function(id){ if(typeof(id)=="object")id=id._hashCode; var idObj=this._obj[id]; if(idObj!=null){ return this._data.indexOf(idObj); } else{return-1;} }; HashList.prototype.remove=function(id){ var data=this._data;var obj=this._obj;var idObj=null; if(typeof(id)=="number"){idObj=data[id];data.splice(id,1);delete obj[idObj._id];} else{if(typeof(id)=="object")id=id._hashCode;idObj=obj[id];delete obj[id];var _index=data.indexOf(idObj);if(_index>=0){data.splice(_index,1);}};return idObj;}; HashList.prototype.clear=function(){this._data=new Array();this._obj=new Object();}; HashList.prototype.size=function(){return this._data.length;};
2、ext_js的扩展实例
这是一个界面调试框的例子,
通过监听ctrl+shift+d来弹出相应的窗口,在窗口中可以输入相关js代码,
运行就能得到相关结果
// ////////////Framelet end//////////////// // ////////////Debugger begin//////////////// dframe.DebuggerWindow=function() { this.winDebugger=null; }; dframe.DebuggerWindow.prototype.show=function() { if(this.winDebugger) { this.winDebugger.show(this); return; }; if (!Ext.Panel || !Ext.Window) { alert("没有找到Ext.Panel、Ext.Window或Ext.Button的定义!"); return; } // 创建调试窗口 var pnlScript = new Ext.Panel({ title : "Script", layout : "fit", region : "center", collapsible: false, margins : "3 0 3 3", cmargins : "3 3 3 3", buttons : [{ text : "Run", id : "btnRun", handler : function() { // 执行脚本 eval(document.getElementById("__txtScript").value); } }], items : [{ xtype : "textarea", id : "__txtScript" }] }); var pnlResult = new Ext.Panel({ title : "Result", region : "south", margins : "3 3 3 0", layout : "fit", height : 100, collapsible: true, split:true, items : [{ xtype : "textarea", id : "__txtResult" }] }); this.winDebugger = new Ext.Window({ title : "Debug console", layout : "border", width : 400, height : 420, closeAction : "hide", items : [pnlScript,pnlResult] }); this.winDebugger.show(this); }; dframe.DebuggerWindow.prototype.out=function(msg) { if(msg==null) { msg=""; }; var txtResult=document.getElementById("__txtResult"); if(txtResult!=null) { if(txtResult.value=="") { txtResult.value=msg; }else { txtResult.value=txtResult.value+"/n"+msg; } } }; Debugger=new dframe.DebuggerWindow(); dbg=Debugger; // ////////////Debugger end//////////////// // ////////////////Pub begin//////////////// /** * 全局的按键事件 * * @param {} * argEvent * @return {Boolean} */ function onDocumentKeyDown(argEvent) { switch(argEvent.keyCode) { case 68:// 键d { if(__CLIENT_DEBUG&&argEvent.ctrlKey&&argEvent.shiftKey) { Debugger.show(); argEvent.cancelBubble=true; return false; }; break; }; }; }; Ext.EventManager.addSystemEvent(document,"onkeydown",onDocumentKeyDown); //事件监听 /* 弹出加载框... */ var __LOADING_TIP_DIV=null; function showLoadingTip() { if(__LOADING_TIP_DIV==null) { __LOADING_TIP_DIV=document.createElement("DIV"); __LOADING_TIP_DIV.style.display="none"; __LOADING_TIP_DIV.style.position="absolute"; __LOADING_TIP_DIV.innerHTML="<TABLE cellSpacing=/"8/"><TR><TD><IMG src="/" mce_src="/"""+__THEME_PATH+"/loading.gif/"></TD>"+"<TD style="/" mce_style="/""white-space: nowrap/">Loading...</TD></TR></TABLE>"; __LOADING_TIP_DIV.className="LoadingTip"; document.body.appendChild(__LOADING_TIP_DIV); }; __LOADING_TIP_DIV.style.display=""; var scrollLeft=document.body.scrollLeft; var scrollTop=document.body.scrollTop; var clientWidth=document.body.clientWidth; var clientHeight=document.body.clientHeight; var offsetWidth=__LOADING_TIP_DIV.offsetWidth; var offsetHeight=__LOADING_TIP_DIV.offsetHeight; __LOADING_TIP_DIV.style.left=scrollLeft+(clientWidth-offsetWidth)/2; __LOADING_TIP_DIV.style.top=scrollTop+(clientHeight-offsetHeight)/2; }; function hideLoadingTip() { if(counter<=0&&__LOADING_TIP_DIV!=null)__LOADING_TIP_DIV.style.display="none"; };
3、其他相关处理的总结
/** * 获取url参数值 * Get a parameter specified in the URL. For example, if a URL is * http://localhost:8080/iportal/wr?__report=/16.webrptdesign&iPortalID=YPTDAGCNPOYSOS * getURLParameter(iPortalID) will return YPTDAGCNPOYSOS */ function getURLParameterValue( url, parameterName ) { var qestionPos=url.indexOf("?"); var str=parameterName+"="; var paramStartIndex = url.indexOf( str,qestionPos ); if (paramStartIndex<0) return null; var paramEndIndex = url.indexOf( "&", paramStartIndex ); var paramString = ""; if ( paramEndIndex >= 0 ) { paramString = url.substring( paramStartIndex, paramEndIndex ); } else { paramString = url.substring( paramStartIndex ); // get the substring // till the end. } var strs = paramString.split("="); if(strs.length>1) return strs[1]; };
这部分代码,以后再慢慢完善
相关字段数据验证处理
function Validator() { this.enabled=true; this.validateImmediately=true; this.errorMessage=null; this._tag; }; Validator.prototype.isEnabled=function () { return this.enabled; }; Validator.prototype.setEnabled=function (formatm) { this.enabled=formatm; }; Validator.prototype.isValidateImmediately=function () { return this.validateImmediately; }; Validator.prototype.setValidateImmediately=function (insertModeX) { this.validateImmediately=insertModeX; }; Validator.prototype.getErrorMessage=function () { return this.errorMessage; }; Validator.prototype.setErrorMessage=function (errorMessage) { this.errorMessage=errorMessage; }; Validator.prototype.getFinalErrorMessage=function () { return this.errorMessage; }; Validator.prototype.$Uw=function () { throw this.getFinalErrorMessage(); }; function RequiredValidator() { this._dFrameClass="RequiredValidator"; this.validateImmediately=false; this.errorMessage=__VALIDATOR_REQUIRED_MESSAGE; }; RequiredValidator.prototype=new Validator(); RequiredValidator.prototype.validate=function (value) { if(typeof(value)=="undefined"||value==null||value.length==0) { return false; }else if(typeof(value)=="string") { var resText=false; for(var i=0;i<value.length;i++) { var $xR=value.charAt(i); if($xR!=' '&&$xR!='/$kX') { resText=true; break; } }; return resText; }else { return true; } }; function LengthValidator() { this._dFrameClass="LengthValidator"; this.validateImmediately=false; this.errorMessage=__VALIDATOR_LENGTH_MESSAGE; this.minLength=0; this.maxLength=0; }; LengthValidator.prototype=new Validator(); LengthValidator.prototype.getMinLength=function () { return this.minLength; }; LengthValidator.prototype.setMinLength=function (minLength) { this.minLength=minLength; }; LengthValidator.prototype.getMaxLength=function () { return this.maxLength; }; LengthValidator.prototype.setMaxLength=function (maxLength) { this.maxLength=maxLength; }; LengthValidator.prototype.validate=function (value) { var length=0; if(value!=null) { var n=parseInt(getPreferenceSetting("__NCHAR_LENGTH",1)); value=new String(value); for(var i=0;i<value.length;i++) { length+=((value.charCodeAt(i)>255)?n:1); } }; if(this.minLength>0&&length<this.minLength) { return false; }; if(this.maxLength>0&&length>this.maxLength) { return false; }; return true; }; var MUTCH_RULE_IGNORE="ignore"; var MUTCH_RULE_EQ="allow_equals"; var MUTCH_RULE_NEQ="not_allow_equals"; function RangeValidator() { this._dFrameClass="RangeValidator"; this.errorMessage=__VALIDATOR_RANGE_MESSAGE; this.minValue=0; this.minMatchRule=MUTCH_RULE_IGNORE; this.maxValue=0; this.maxMatchRule=MUTCH_RULE_IGNORE; }; RangeValidator.prototype=new Validator(); RangeValidator.prototype.getMin=function () { return this.minValue; }; RangeValidator.prototype.setMin=function (minValue) { this.minValue=minValue; }; RangeValidator.prototype.getMinMatchRule=function () { return this.minMatchRule; }; RangeValidator.prototype.setMinMatchRule=function (minMatchRule) { this.minMatchRule=minMatchRule; }; RangeValidator.prototype.getMax=function () { return this.maxValue; }; RangeValidator.prototype.setMax=function (maxValue) { this.maxValue=maxValue; }; RangeValidator.prototype.getMaxMatchRule=function () { return this.maxMatchRule; }; RangeValidator.prototype.setMaxMatchRule=function (maxMatchRule) { this.maxMatchRule=maxMatchRule; }; RangeValidator.prototype.validate=function (value) { var n=parseFloat(value); if(isNaN(n))n=0; var flag=false; if(this.minMatchRule==MUTCH_RULE_EQ) { if(!(n>=this.minValue)) { flag=true; } }else if(this.minMatchRule==MUTCH_RULE_NEQ) { if(!(n>this.minValue)) { flag=true; } }; if(this.maxMatchRule==MUTCH_RULE_EQ) { if(!(n<=this.maxValue)) { flag=true; } }else if(this.maxMatchRule==MUTCH_RULE_NEQ) { if(!(n<this.maxValue)) { flag=true; } }; if(flag) { return false; }; return true; }; function ListValidator() { this._dFrameClass="ListValidator"; this.errorMessage=__VALIDATOR_LIST_MESSAGE; this.values=null; this.valueList=null; }; ListValidator.prototype=new Validator(); ListValidator.prototype.getValues=function () { return this.maxValue; }; ListValidator.prototype.setValues=function (value) { this.values=value; if(value!=null) { this.valueList=value.split(","); }else { this.valueList=null; } }; ListValidator.prototype.validate=function (value) { var s=""; if(value!=null) { s=value.toString (); }; if(s.length>0&&this.valueList!=null&&this.valueList.length>0) { for(var i=0;i<this.valueList.length;i++) { if(this.valueList[i]==s)return true; }; return false; } return true; }; function PatternValidator() { this._dFrameClass="PatternValidator"; this.errorMessage=__VALIDATOR_PATTERN_MESSAGE; this.pattern=null; this.reG=null; }; PatternValidator.prototype=new Validator(); PatternValidator.prototype.getPattern=function () { return this.pattern; }; PatternValidator.prototype.setPattern=function (patten) { if(patten) { var reg=new RegExp(); reg.compile(patten); this.reG=reg; }else { this.reG=null; }; this.pattern=patten; }; PatternValidator.prototype.validate=function (value) { var s=""; if(value!=null) { s=value.toString (); var reg=this.reG; if(s.length>0&®!=null) { return reg.test(s); } }; return true; }
相关文章推荐
- JSF的各种框架实现(对目前JSF群雄一个了解)
- 用过滤器解决jsp乱码问题!!
- 关于IE8无法获取更新后的js文件
- web前端开发书籍推荐(之JavaScript )
- 前端开发者应该关注的JavaScript学习资源
- Js实现窗口的的显示和拖动
- 高效 JavaScript(转贴)
- ExtJs动态生成含有checkBox的Tree
- Web前端开发规范文档(css/javascript)
- Javascript中的函数声明和函数表达式
- javascript 类
- JavaScript奇技淫巧之遍历数组
- ExtJSweb应用程序开发指南的一个例子分享-ComboBox的扩展
- 判断JS对象是否拥有某属性
- jsp理解
- servlet和jsp结合
- ASP.NET JavaScript显示动态时间
- JSP分页技术
- JS选择select
- gridview中Checkbox全选或全取消的js代码