这个函数我觉得还是比较实用的,用于69dns.com的一个新项目。代码有点熟悉?不过这个可是我原创的代码哦。
还有一个控制xml,控制form,控制css,以及加载的boot.js。
当然还有很多东西都不完善,等以后有空的时候,都整理一下。
实际上只是创建了一个stcms的json对象。为了能独立使用加入了if(typeof(stcms)==”undefined”)stcms={};,算是另类的命名空间吧。
/* 文件:ajax.js 挂载:stcms 独立:可独立使用 设计:猫七@69dns.com */ (function(){ if(typeof(stcms)=="undefined")stcms={}; stcms.ajaxstatus=0; stcms.ajax=function(url,str,method,callback){ if(stcms.ajaxstatus==1){ alert("请等待上一个进程处理完。"); return; } if(url.indexOf("?")!=-1)url+="&sendTme="+new Date();else url+="?sendTme="+new Date(); var xmlhttpRequest; if(typeof(xmlhttpRequest!="object")){ if(window.ActiveXObject){ try{ xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ xmlhttpRequest = false; } }else if(window.XMLHttpRequest){ xmlhttpRequest = new XMLHttpRequest(); } } if(xmlhttpRequest){ stcms.ajaxstatus=1; xmlhttpRequest.open(method,url,true); if(method=="post")xmlhttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttpRequest.setRequestHeader("charset","gb2312"); xmlhttpRequest.onreadystatechange=function(){ if(xmlhttpRequest.readyState==4){ if(xmlhttpRequest.status==200){ callback(xmlhttpRequest.responseText); stcms.ajaxstatus=0; } } }; xmlhttpRequest.send(str); } } })();
使用方法很简单:
stcms.ajax('a.asp',null,'get',function(t){alert(t)});
很简单吧。另外发一下我boot.js,我觉得还是比较实用的;
/* 文件:boot.js 挂载:root 功能:加载js 设计:猫七@69dns.com */ (function(skin){ var i,ie,d; stcms={ listen:function(f){ var l; window.onload=(typeof(l=window.onload)!='function')?f:function(){l();f();}; }, $:function(id){ return document.getElementById(id); } }; ie=!!((d=(document)).all); (function(arr){ for(i in arr){ var o=d.createElement("script"); o.setAttribute("type","text/javascript"); o.setAttribute("src",arr[i].replace("stcms.","js/").replace(/\./g,"\/").replace("*","all")+".js") d.getElementsByTagName('head')[0].appendChild(o); } })([ 'stcms.common.client.mouse', 'stcms.common.client.keyboard', 'stcms.skin.'+skin, 'stcms.common.form.form', 'stcms.common.form.grid', 'stcms.common.form.box', 'stcms.common.ajax', 'stcms.common.xml', 'stcms.common.cookie', 'stcms.user.checklogin', 'stcms.control.init' ]); })('default');
现在boot.js已经实现了皮肤,listen(实际就是window.onload),加载js,通过id获取dom等四个功能。
其他的可能就不太常用的,吧所有的都细分,这样可以减少请求和文件大小哦。
‘stcms.common.ajax’,就是上边的ajax.js。
如果有时间,我也准备写个js框剪,集合了所有的功能,哈哈哈。