常用js代码公用方法

常用js代码公用方法

//字符串长度 var temp=true; String.prototype.len = function() { var str=this; return str.replace(/[^/x00-/xff]/g, "**").length; } //裁剪两端空格 String.prototype.trim = function() { return this.replace(/(^/s*)|(/s*$)/g, ""); } //取得地址栏参数 var Request = { QueryString : function(item,paramurl) { if(typeof(paramurl)!="string") paramurl=location.search; var svalue = paramurl.match(new RegExp("[/?/&]" + item + "=([^/&]*)(/&?)","i")); var Qvalue = svalue ? svalue[1] : svalue; if(Qvalue==null) { return ""; } else { return Qvalue.trim(); } } } //取得顶窗口地址栏参数 var topRequest = { QueryString : function(item) { return Request.QueryString(item,top.location.search); } } //取得HTML元素 function $(node_id) { return document.getElementById(node_id); } //取得XMLHttp对象 function newXMLHttp() { var XMLHttp=null; if (window.XMLHttpRequest) { XMLHttp=new XMLHttpRequest(); } if(XMLHttp==null && window.ActiveXObject) { var clsids = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.2.0","MSXML2.XMLHttp.1.0","MSXML2.XMLHttp","Microsoft.XMLHttp"]; for(var i=0; i<clsids.length && XMLHttp == null; i++) { try { XMLHttp = new ActiveXObject(clsids[i]); } catch(e) {} } } return XMLHttp; } //POST页面 function postHTML(URL,data,func) { try { var XMLHttp=newXMLHttp(); //同步调用 if(typeof(func)!="function") { XMLHttp.open("POST",URL, false); XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); XMLHttp.send(data); var result = XMLHttp.status; var responseText=XMLHttp.responseText; if(result==200) { return responseText; } else { return false; } } //异步调用 else { temp=false; XMLHttp.onreadystatechange = function() { if (XMLHttp.readyState == 4) { temp=true; if (XMLHttp.status == 200) { var responseText = XMLHttp.responseText ; func(responseText); } else { func(false); } } } XMLHttp.open("POST",URL, true); XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); XMLHttp.send(data); } } catch(e){} } //禁用F5,退格键 function forbidF5(event) { var event = event || window.event; var evtnode=event.srcElement?event.srcElement:event.target; var key = event.keyCode || event.which; //116 F5 117 F6 8 BACKSPACE if(key==116 || key==117 || (key == 8 && evtnode.nodeName != "TEXTAREA" && evtnode.nodeName != "INPUT") ) { eventstop(event); } else { return true; } } function eventstop(event) { try{event.keyCode=0;} catch(err) {} try{event.preventDefault();}catch(err){} try{event.stopPropagation();}catch(err){} try{event.returnValue = false;}catch(err){} try{event.cancelBubble = true;} catch(err){} return false; } 

你可能感兴趣的:(function,String,null,url,input,XMLhttpREquest)