实现自己的Ajax对象封装器 -- Kajax --第一版完成

21 篇文章 0 订阅
17 篇文章 0 订阅

第一版 浏览: http://www.iamsese.cn/static/forwardAction.php?action=Kajax

/**
 * Kajax 是自定义的 Ajax封装器
 */
function Kajax(){
	this.XMLHttp = this.getXMLHttpObject();	
}

Kajax.prototype.getXMLHttpObject = function() {
	//define a bool paramter to check IE instance
	var xmlhttp = false ;
	
	//check client brower is IE
	try {
	    // If javascript is greater than 5
	    xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
	    //alert("You are using Microsoft Internet Explorer .");
	}
	catch (e){
	    //else will use ActiveXObject older version
	    try {
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        //alert("You are using old Microsoft Internet Explorer .");
	    }
	    catch (e){
	         //using brower is no IE.
	         xmlhttp = false ;
	    }
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
	    	xmlhttp = new XMLHttpRequest();
	    	//alert("You are not using Microsoft Internet Explorer .");
		}
		catch(e){
			xmlhttp = false ;
		}
	}
	return xmlhttp ;	
}

Kajax.prototype.get = function (url,isAsyc){
	var outstring = '' ;
	if (isAsyc==undefined) isAsyc=false ;
	if (this.XMLHttp) {		
		this.XMLHttp.open("GET",url,isAsyc);
		var oThis = this.XMLHttp ; //这里复制oThis变量的作用是 不与onreadystatechange函数中的this域冲突
		this.XMLHttp.onreadystatechange = function(){
			if (oThis.readyState == 4 && oThis.status == 200 )
				outstring += oThis.responseText ;
		}
		this.XMLHttp.send(null) ;
	}
	return outstring ;
}
/**
 * 同步获取一个JS文件,并执行
 * @param {} url
 */
Kajax.prototype.getJS = function (url){
	eval(this.get(url));
}

 

  • 大小: 101.2 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值