一是AJAX
二是ELEMENTS
AJAX是做一個簡單的FUNCTION,讓我可以讀取資料
它的主要FUNCTION只有二個
getData和getText 同步和非同步
使用方法
var menu1=new awp_tablemaker();
var k1=menu1.getData("&username=eddie","getusername.php");
alert(k1.responseXML);
alert(k1.responseText);
ELEMENTS也是只有二個FUNCTION
getvalue和set 讀取資料和設定資料
使用方法(INCLUDE後)
var e1=new awp_elements();
e1.set("sid","123");
var k1=e1.getvalue("sid");
sida是欄位名稱(name),例如下面六項都可以讀到
後來又加了一項
<span id="span_sid">
awp_ajax.js
if (typeof(chkAjaBrowser)=="undefined"){
function chkAjaBrowser()
{
var a,ua = navigator.userAgent;
this.bw= {
safari : ((a=ua.split('AppleWebKit/')[1])?a.split('(')[0]:0)>=124 ,
konqueror : ((a=ua.split('Konqueror/')[1])?a.split(';')[0]:0)>=3.3 ,
mozes : ((a=ua.split('Gecko/')[1])?a.split(" ")[0]:0) >= 20011128 ,
opera : (!!window.opera) && ((typeof XMLHttpRequest)=='function') ,
msie : (!!window.ActiveXObject)?(!!awp_ajax.createHttpRequest):false
}
return (this.bw.safari||this.bw.konqueror||this.bw.mozes||this.bw.opera||this.bw.msie)
}
}
if (typeof(awp_ajax)=="undefined"){
awp_ajax=function(){
this.createHttpRequest=function(){
if(window.XMLHttpRequest){
return new XMLHttpRequest() ;
} else if(window.ActiveXObject){
try {
return new ActiveXObject("Msxml2.XMLHTTP") ;
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP") ;
} catch (e2) {
return null ;
}
}
} else {
return null ;
}
}
this.sendRequest=function(callback,data,method,url,async,sload,user,password){
var oj = this.createHttpRequest();
if( oj == null ) return null;
var sload = (!!this.sendRequest.arguments[5])?sload:false;
if(sload || method.toUpperCase() == 'GET')url += "?";
if(sload)url=url+"t="+(new Date()).getTime();
var bwoj = new chkAjaBrowser();
var opera = bwoj.bw.opera;
var safari = bwoj.bw.safari;
var konqueror = bwoj.bw.konqueror;
var mozes = bwoj.bw.mozes ;
if(typeof callback=='object'){
var callback_onload = callback.onload;
var callback_onbeforsetheader = callback.onbeforsetheader;
} else {
var callback_onload = callback;
var callback_onbeforsetheader = null;
}
if(opera || safari || mozes){
oj.onload = function () { callback_onload(oj); }
} else {
oj.onreadystatechange =function ()
{
if ( oj.readyState == 4 ){
callback_onload(oj);
}
}
}
data = this.uriEncode(data,url);
if(method.toUpperCase() == 'GET') {
url += data;
}
oj.open(method,url,async,user,password);
if(!!callback_onbeforsetheader)callback_onbeforsetheader(oj);
this.setEncHeader(oj);
oj.send(data);
return oj
}
this.setEncHeader=function(oj){
var contentTypeUrlenc = 'application/x-www-form-urlencoded; charset=UTF-8';
if(!window.opera){
oj.setRequestHeader('Content-Type',contentTypeUrlenc);
} else {
if((typeof oj.setRequestHeader) == 'function')
oj.setRequestHeader('Content-Type',contentTypeUrlenc);
}
return oj
}
this.uriEncode=function(data,url){
var encdata =(url.indexOf('?')==-1)?'?dmy':'';
if(typeof data=='object'){
for(var i in data)
encdata+='&'+encodeURIComponent(i)+'='+encodeURIComponent(data[i]);
} else if(typeof data=='string'){
if(data=="")return "";
var encdata = '';
var datas = data.split('&');
for(i=1;i
var dataq = datas[i].split('=');
encdata += '&'+encodeURIComponent(dataq[0])+'='+encodeURIComponent(dataq[1]);
}
}
return encdata;
}
this.getText=function(){
if (arguments.length >= 3){
var temp_argu="";
for (var tempi=3;tempi < arguments.length; tempi++){
temp_argu=temp_argu + ",'" + arguments[tempi] +"'" ;
}
var temp_func1=arguments[2] + "(arguments[0]" + temp_argu + ");";
var temp_func2=new Function(temp_func1);
this.sendRequest(temp_func2,arguments[0],'POST',arguments[1],true,true);
}
}
this.getData=function(){
if (arguments.length == 2){
var temp_func2=new Function("return false;");
return this.sendRequest(temp_func2,arguments[0],'POST',arguments[1],false,true);
}else{
alert("null");
return null;
}
}
}//awp_ajax
}//typeof
awp_elements.js
if (typeof(awp_elements)=="undefined"){
awp_elements=function(){
this.noexist=1;
this.sourcedocument=document;
this.sp=", ";
this.getFromXML=function(theXML)
{
if (theXML.hasChildNodes())
{
var tempt2=theXML.childNodes;
for (var tempi2=0;tempi2 < tempt2.length;tempi2++)
{
if (tempt2[tempi2].nodeType==1)
{
tempt1=tempt2[tempi2].childNodes;
for (var tempi=0;tempi < tempt1.length;tempi++)
{
if (tempt1[tempi].nodeType==1)
{
if (tempt1[tempi].getAttribute("addition")!="yes")
{
var tempvalue=(tempt1[tempi].hasChildNodes()) ? tempt1[tempi].firstChild.nodeValue :"";
this.set(tempt1[tempi].nodeName,tempvalue);
}
}
}
}
}
}
}
this.putinXML=function(theXML)
{
if (theXML.hasChildNodes())
{
var tempt2=theXML.childNodes;
for (var tempi2=0;tempi2 < tempt2.length;tempi2++)
{
if (tempt2[tempi2].nodeType==1)
{
tempt1=tempt2[tempi2].childNodes;
for (var tempi=0;tempi < tempt1.length;tempi++)
{
if (tempt1[tempi].nodeType==1)
{
if (tempt1[tempi].getAttribute("addition")!="yes")
{
tempt1[tempi].firstChild.nodeValue=this.getvalue(tempt1[tempi].nodeName);
}
}
}
}
}
}
}
this.setvalue=function(fname,fvalue){
return this.set(fname,fvalue);
}
this.set=function(fname,fvalue){
//不是input標簽的欄位要顯示前面要加span_
//此段程式會先做一次
//但是欄位如果要儲存會抓不到值,需增加一個隱藏欄位
var temp_k0=document.getElementById("span_"+fname);
if (temp_k0!=null) {temp_k0.innerHTML=fvalue;}
var temp_k1=this.elements(fname);
if (temp_k1==null){return false;}
if (fvalue==null){fvalue="";}
for (f_i=0;f_i < temp_k1.length; f_i++)
{
var tempa1=temp_k1[f_i].getAttribute("beforeset");
if (tempa1!=null && tempa1!="")
{
tempa1=tempa1.replace(/\{fname}/g,fname);
try{
var tempf1= new Function(tempa1);
tempf1();
}catch(e){alert(fname + " function error:\n" + tempa1);}
}
//input type
if (temp_k1[f_i].type=='text'){this.input_text(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='radio'){this.input_radio(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='checkbox'){this.input_checkbox(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='hidden'){this.input_hidden(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='password'){this.input_password(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='select-one'){this.selectone(temp_k1[f_i],fvalue);}
else if (temp_k1[f_i].type=='textarea'){this.textarea(temp_k1[f_i],fvalue);}
var tempa1=temp_k1[f_i].getAttribute("afterset");
if (tempa1!=null && tempa1!="")
{
tempa1=tempa1.replace(/\{fname}/g,fname);
try{
var tempf1= new Function(tempa1);
tempf1();
}catch(e){alert(fname + " function error:\n" + tempa1);}
}
}
}
this.input_text=function(felement,fvalue){
felement.setAttribute("value",fvalue);
felement.value=fvalue;
}
this.input_password=function(felement,fvalue){
this.input_text(felement,fvalue);
}
this.input_hidden=function(felement,fvalue){
this.input_text(felement,fvalue);
}
this.textarea=function(felement,fvalue){
//felement.innerHTML=fvalue;
this.input_text(felement,fvalue);
}
this.selectone=function(felement,fvalue){
var temp_t4=(arguments.length > 2) ? arguments[2]:this.sp;
var fvalue2=fvalue;
fvalue2=fvalue2 + temp_t4;
var temp_k3=felement.options;
for (var f_j=0;f_j < temp_k3.length; f_j++){
temp_t3=temp_k3[f_j].value;
if (temp_t3==""){temp_t3=temp_k3[f_j].text;}
temp_t3=temp_t3 + temp_t4;
if (fvalue2.indexOf(temp_t3,0) > -1){
temp_k3[f_j].setAttribute("selected","true");
temp_k3[f_j].selected=true;
}else{
temp_k3[f_j].selected=false;
}
}
}
this.input_radio=function(felement,fvalue){
var fvalue2=fvalue;
var temp_k3=felement.value;
if (fvalue2==temp_k3){
felement.setAttribute("checked","true");
felement.checked=true;
}else{
felement.checked=false;
}
}
this.input_checkbox=function(felement,fvalue){
var temp_t4=(arguments.length > 2) ? arguments[2]:this.sp;
var fvalue2=fvalue + temp_t4;
var temp_k3=felement.value + temp_t4;
if (fvalue2.indexOf(temp_k3,0) > -1){
felement.setAttribute("checked","");
felement.checked=true;
}else{
felement.removeAttribute("checked");
felement.checked=false;
}
}
this.add=function(fname,fvalue,ftext){
var temp_k1=this.elements(fname);
if (temp_k1==null){return false;}
if (temp_k1.type!="select-one"){
var temp_k3=fvalue;
var temp_t3=(ftext!=null)?ftext:temp_k3;
var temp_t4=temp_k1[0].options;
temp_t4.add(new Option(temp_k3,temp_t3));
temp_k1[0].options[temp_k1[0].options.length-1].value=temp_k3;
temp_k1[0].options[temp_k1[0].options.length-1].text=temp_t3;
}else{
if (this.noexist==1){alert("This element is not select--" + fname)};
}
}
this.add_select=function(url,attr,elem)
{
//SELECT的資料從XML寫入
var a1=new awp_ajax();
var e1=new awp_elements();
var ob=a1.getData(attr,url);
//alert(ob.responseText);
var temp1=ob.responseXML.getElementsByTagName("data");
for (var tempi=0;tempi < temp1.length;tempi++)
{
var temp2=temp1[tempi].getElementsByTagName("fname");
var fname="";
if (temp2.length >0) {fname=temp2[0].firstChild.nodeValue;}
var temp2=temp1[tempi].getElementsByTagName("fvalue");
var fvalue="";
if (temp2.length >0) {fvalue=temp2[0].firstChild.nodeValue;}
else {fvalue=fname;}
if (fname !="")
{
e1.add(elem,fname,fvalue);
}
}
}
this.getvalue=function(fname){
var temp_k1=this.elements(fname);
if (temp_k1==null){return false;}
var temp_t4=(arguments.length > 1) ? arguments[1]:this.sp;
var temp_t5="";
for (f_i=0;f_i < temp_k1.length; f_i++){
var temp_k3=temp_k1[f_i].type;
var k9=temp_k1[f_i];
var temp_t3="";
if (temp_k3=='text' || temp_k3=='password' || temp_k3=='hidden' || temp_k3=='textarea'){
temp_t3=k9.value;
}else if (temp_k3=='radio' || temp_k3=='checkbox'){
if (k9.checked==true){
temp_t3=k9.value;
}
}else if (temp_k3=='select-one'){
for (f_j=0;f_j < k9.options.length; f_j++){
if (k9.options[f_j].selected==true){
if (temp_t3 !=""){temp_t3=temp_t3 + temp_t4;}
temp_t3=temp_t3 + k9.options[f_j].value;
}
}
}
if (temp_t3!="")
{
if (temp_t5 !=""){temp_t5=temp_t5 + temp_t4;}
temp_t5=temp_t5 + temp_t3;
}
}
return temp_t5;
}
this.elements=function(fname){
var temp_k1=this.sourcedocument.getElementsByName(fname);
if (temp_k1.length <1){
if (this.noexist==1){ alert("can not fine this element--" + fname); }
return null;
}else{
return temp_k1;
}
}
this.setbox=function(fname,fflag){
var temp_k1=this.elements(fname);
if (temp_k1==null){return false;}
for (f_i=0;f_i < temp_k1.length; f_i++){
switch(fflag){
case 0:
temp_k1[f_i].checked=false;break;
case 1:
temp_k1[f_i].checked=true;break;
case 2:
temp_k1[f_i].checked=(temp_k1[f_i].checked)?false:true;
break;
}
}
}
}//awp_elements
}//typeof