这样的封装方式可以运行:
*********************************
Default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">function doQry()
{
var tmpObj=new oAjax();
tmpObj.send('Default2.aspx',function()
{
if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status==200)
{
alert("你好:" + tmpObj.oSend.responseText);
}
else if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status!=200){alert('error');}
})
}
</script>
************************************
test.js
function createXMLHttpRequest()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
function oAjax()
{
this.oSend= new createXMLHttpRequest();
}
oAjax.prototype.send=function(url,handler)
{
this.oSend.open("GET",url,true);
this.oSend.onreadystatechange=handler;
this.oSend.setRequestHeader("If-Modified-Since","0");
this.oSend.send(null);
}但是我现在在test.js中如果不要this.oSend对象,这样封装,提示出错,请问为什么?
*******************
default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<div id="qryResult"></div>
<script src="test.js"></script>
<script language="javascript">
var m=new oAjax();
function doQry()
{
m.send('Default2.aspx',xx); //这里报错,说找不到m.send方法!!!!
}
function xx()
{ }
</script>***********************
test.jsfunction oAjax()
{
return new this.createXMLHttpRequest();
}
oAjax.prototype.createXMLHttpRequest=function()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
oAjax.prototype.zz=function(a)
{
return a;
}
oAjax.prototype.send=function(url,handler)
{
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}
*********************************
Default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">function doQry()
{
var tmpObj=new oAjax();
tmpObj.send('Default2.aspx',function()
{
if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status==200)
{
alert("你好:" + tmpObj.oSend.responseText);
}
else if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status!=200){alert('error');}
})
}
</script>
************************************
test.js
function createXMLHttpRequest()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
function oAjax()
{
this.oSend= new createXMLHttpRequest();
}
oAjax.prototype.send=function(url,handler)
{
this.oSend.open("GET",url,true);
this.oSend.onreadystatechange=handler;
this.oSend.setRequestHeader("If-Modified-Since","0");
this.oSend.send(null);
}但是我现在在test.js中如果不要this.oSend对象,这样封装,提示出错,请问为什么?
*******************
default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<div id="qryResult"></div>
<script src="test.js"></script>
<script language="javascript">
var m=new oAjax();
function doQry()
{
m.send('Default2.aspx',xx); //这里报错,说找不到m.send方法!!!!
}
function xx()
{ }
</script>***********************
test.jsfunction oAjax()
{
return new this.createXMLHttpRequest();
}
oAjax.prototype.createXMLHttpRequest=function()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
oAjax.prototype.zz=function(a)
{
return a;
}
oAjax.prototype.send=function(url,handler)
{
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}
解决方案 »
- javascript中获取最后一个子节点为空或不是对象
- 兼容四大浏览器 hasOwnProperty判断对象是否有指定的属性和方法!怎么做 啊~ (抱歉分不多)
- jsp中JQUERY 如何获得 CHECKBOX是否被选中?--不使用ONCLICK属性
- 关于jquery回调函数的问题 求高手解答
- javascript #号的意思
- 如何禁用IE浏览器的关闭功能
- 求助,关于select下拉框快速定位的问题
- 一个关于目录树的问题,请指教!!
- 能写个将12345678.99转换成12,345,678.99 的javascript金额处理函数吗,巨急
- 根据日期显示提示信息
- 如何让一个浏览器窗口置顶
- javascript 菜鸟问this的含义!!
oAjax.prototype.send=function(url,handler){
this.open("GET",url,true);
this.onreadystatechange=handler;
this.setRequestHeader("If-Modified-Since","0");
this.send(null);
}
//构造函数中return object将覆盖new结果,改变实例的constructor从而无法保持prototype链
function oAjax(){
return new this.createXMLHttpRequest();
} 修正方法:
function oAjax(){
this.ajax = new this.createXMLHttpRequest();//将XMLHTTP对象赋给属性
}oAjax.prototype.send=function(url,handler){//send方法不再混乱
this.ajax.open("GET",url,true);
this.ajax.onreadystatechange=handler;
this.ajax.setRequestHeader("If-Modified-Since","0");
this.ajax.send(null);
}
不过我自创的下面的写法更容易理解.更接近与C语言的Class
1.function即class 2.function不能直接嵌套function,必须声明个变量,然后赋值为函数
test.js
function Ajax()
{
this.request=function()
{
try
{
if (window.XMLHTTPRequest) return new XMLHttpRequest();
else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
this.oAjax= new this.request();
this.send=function(url,handler)
{
this.oAjax.open("GET",url,true);
this.oAjax.onreadystatechange=handler;
this.oAjax.setRequestHeader("If-Modified-Since","0");
this.oAjax.send(null);
}
} test.htm
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">
function doQry()
{
var m=new Ajax();
m.send('default2.aspx',function()
{
if(m.oAjax.readyState==4 && m.oAjax.status==200)
{
alert("你好:" + m.oAjax.responseText);
}
else if(m.oAjax.readyState==4 && m.oAjax.status!=200){alert('error');}
})
}
</script>