Javascript Ajax Sample HTML

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

AjaxTest.

html Page 1
1 <html><head><script language="JavaScript" type="text/javascript">
/// AjaxClass 200907
/// powered by Takeshi Aoyagi
/// http://www.spreadclouds.com/
- function Ajax(url, target){
this._Set(url, target);
};
Ajax.prototype = {
_Set:/// url:システムURL、target:HTMLタグID
10 function(url, target){
if(!this.Adapter){
while(this._Res.ADAPTERS.length >= 0){
try{
this.Adapter = eval(this._Res.ADAPTERS.shift());
- break;
}catch(e){}
};
this.Url = url;
this.Target = document.getElementById(target);
20 this.Adapter.prototype = {Owner:null};
}
},

Send:/// type:['GET','POST']インデックス、data:送信する文字
- function(type, data){
if(this.Adapter){
this.Adapter.onreadystatechange = this._CallBack;
this.Adapter.Owner = this;
try{
30 this.Adapter.open(this._Res.QUERYTYPE[type], this.Url, true);
this.Adapter.send(data);
this.Target.innerHTML = this._Res.READ;
}catch(e){
this.Target.innerHTML = this._Res.READ_ERROR.replace('%', e);
- }
}else{
this.Target.innerHTML = this._Res.NOREAD;
}
},
40
_CallBack:///アダプタ内のコールバック用
function(){
this.Owner.Target.innerHTML += this.Owner._Res.READING;
if(this.readyState == 4){
- if(this.status == 200 || (this.status == 0 && this.responseText != '')){
this.Owner.Target.innerHTML = this.responseText;
}else{
this.Owner.Target.innerHTML = this.Owner._Res.HTTP_ERROR.replace('%', this.status);
}
50 }
},

_Res:/// リソース
{
- READ: '読み込み中…',
NOREAD: '読み込めません',
READ_ERROR: '読み込み失敗「%」',
HTTP_ERROR: '読み込み失敗(Code:%)',
READING: '…',
60
ADAPTERS:/// 利用可能なアダプタ一覧
[ "new XMLHttpRequest()",
"new ActiveXObject('Msxml2.XMLHTTP')",
"new ActiveXObject('Microsoft.XMLHTTP')",
- "null"],
QUERYTYPE:/// データ送信方法
[ 'GET','POST']
}
70 };
</script>
</head><body><div id="DataView" class="window"></div>
<script language="JavaScript" type="text/javascript">
var a = new Ajax('ajaxresponse.txt', 'DataView');
- </script>
<input type="button" onclick="a.Send(0,null);" value="Submit"></body></html>

You might also like