高手们帮我改造一个方法下 本帖最后由 tiancaolin 于 2010-12-02 11:38:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 扩展需求:1.AjaxHelper.getScript()方法中的callback方法 我想外界传递参数进去,要怎么传递。2.我在html中创建<script></script>,又该如何将远程服务器上的 信息在callback方法中处理=======================帖子描述错了重新更正需求:就是怎么在callback方法中调用远程服务器的那些数据。请举例下,我尝试不可以 AjaxHelper.getScript({ url:"http://data.585.com/opendatahandler.ashx?action=klinelistflashdatajson", data:"keyword=sh600036", callback:function(e){ // 看看e有没有东西 alert("执行完毕"); } }); 为方便调试,我整理下完整可调试代码,快刀斧,你帮我看看下js代码: var AjaxHelper = { charset: "utf-8", data: "", url: location.href, msg: "", callback: function () { }, getHttpRequest: function () { var xmlhttp = null; if (window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (ex) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } } else if (window.XMLHttpRequest) { try { xmlhttp = new XMLHttpRequest(); } catch (ex) { xmlhttp = window.createRequest(); } if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); } return xmlhttp; }, getRandomNumber: function () { var date = new Date(); return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()) + Math.random().toString().replace(".", ""); }, getScript: function (ajaxsettingcfn, callback) { if (typeof ajaxsettingcfn == "function") { callback = ajaxsettingcfn; ajaxsettingcfn = null; } if (ajaxsettingcfn) { if (ajaxsettingcfn.url) { this.url = ajaxsettingcfn.url; } if (ajaxsettingcfn.charset) { this.charset = ajaxsettingcfn.charset; } if (ajaxsettingcfn.data) { if (ajaxsettingcfn.data.substr(ajaxsettingcfn.data.length - 1) == "&") { this.data += ajaxsettingcfn.data; } else { this.data += "&" + this.data; } this.url += this.data; } } var head = document.getElementsByTagName("head")[0] || document.documentElement; var script = $C("script"); script.charset = this.charset; script.id = "STOCKWARNSCRIPTTAG" script.type = "text/javascript"; script.src = this.url + "&rn=" + this.getRandomNumber(); head.appendChild(script); document.body.appendChild(script); script.onload = script.onreadystatechange = function () { if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") { if (callback) { callback(); } script.onload = script.onreadystatechange = null; $G("STOCKWARNSCRIPTTAG").parentNode.removeChild($G("STOCKWARNSCRIPTTAG")); } }; } } html代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>股票预警测试页面</title> <style type="text/css"> .menu-post { background:none repeat scroll 0 0 #0673B5; border:2px solid #A9DAF7; padding:1px; } .menu { background:none repeat scroll 0 0 #FFFFFF; border:1px solid #A9DAF7; position:absolute; } .menu-post .menu-b { background:none repeat scroll 0 0 #F8F8F8; border:1px solid #A9DAF7; } .tac { text-align:center; } .menu-b .h {border-bottom:1px solid #9DB3C5;} .h {background:none repeat scroll 0 0 #1395E0;border-bottom:1px solid #9DB3C5;color:#FFFFFF;line-height:30px;padding:0 10px;text-align:left;}.h span a, .h span {color:#336699;}.h span {font-weight:normal;} </style> <script src="Scripts/StockWarn.js" language="javascript" type="text/javascript"></script>--> <script language="javascript" type="text/javascript" >//远程URL地址:http://data.585.com/opendatahandler.ashx?action=openstockdata&source=stockquotation_only&keyword=sh600036 window.onload = function () { var stockid = "sh600036"; AjaxHelper.getScript({ url: "http://data.585.com/opendatahandler.ashx?action=openstockdata", data: "source=stockquotation_only&keyword=" + stockid }, function () { alert(serverState); //这里会报错,没定义,但你打开远程URL地址是有的,重点问题是解决这个 alert("执行完毕:" + stockid); }); } </script> </head><body><a href="javascript:;">sh600031</a><a href="javascript:;">测试1</a><a href="javascript:;">600031</a><a href="javascript:;" id="test222" >123</a> </body></html> 另外IE可以 有效触发 if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") { if (callback) { callback(); }里面的callback方法;但firefox好像不行,不知道什么问题,疯掉了真TM的敲一个兼容问题就出来 if (typeof $G == 'undefined') $G = function (id) { return document.getElementById(id); };if (typeof $C == 'undefined') $C = function (t) { return document.createElement(t); }; AjaxHelper.getScript() 这个执行完了有没返回结果? 暴汗啊。 this.data += "&" + this.data;这个地方写错了。。哈哈,每次快刀斧提的我去调试那方面都很有收获现在IE已经彻底解决了但FireFox似乎无法触发 if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {无法触发这里面的方法} this.data += "&" + this.data; 应该写成 this.data += "&" + ajaxsettingcfn.data; if(typeof(callback)=='function'){ if( document.all ){ script.onreadystatechange = function(){ if(/(complete|loaded)/.test(this.readyState)){ callback(); script.onreadystatechange = null; script.parentNode.removeChild(script); }}; }else{ script.onload = function(){ callback(); script.onload = null; script.parentNode.removeChild(script); }; } } 求助:div加载页面。。。 求个思路 看看这个函数 为什么在 IE 下无法用 function getEvent() 为什么同一个控件,有时可用,用时又说“未定义”的呢? 转载:探秘IE8的强大JavaScript功能 关于点击表格内文字链接的问题? 如何在一个TextBox中绑定多个事件? javascript如何删除xml文件的指定节点 怎样能够在系统处理之后处理按键消息 JS如何操作iframe中某元素绑定的事件 求一个动态tabel 和动态增加td 内容的代码 如何根据option的value 获取它的text
1.AjaxHelper.getScript()方法中的callback方法 我想外界传递参数进去,要怎么传递。
2.我在html中创建<script></script>,又该如何将远程服务器上的 信息在callback方法中处理
=======================
帖子描述错了
重新更正需求:
就是怎么在callback方法中调用远程服务器的那些数据。请举例下,我尝试不可以
url:"http://data.585.com/opendatahandler.ashx?action=klinelistflashdatajson",
data:"keyword=sh600036",
callback:function(e){
// 看看e有没有东西
alert("执行完毕");
}
});
js代码: var AjaxHelper = {
charset: "utf-8",
data: "",
url: location.href,
msg: "",
callback: function () { },
getHttpRequest: function () {
var xmlhttp = null;
if (window.ActiveXObject) {
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (ex) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
}
else if (window.XMLHttpRequest) {
try { xmlhttp = new XMLHttpRequest(); }
catch (ex) { xmlhttp = window.createRequest(); }
if (xmlhttp.overrideMimeType)
xmlhttp.overrideMimeType('text/xml');
}
return xmlhttp;
},
getRandomNumber: function () {
var date = new Date();
return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()) + Math.random().toString().replace(".", "");
}, getScript: function (ajaxsettingcfn, callback) { if (typeof ajaxsettingcfn == "function") {
callback = ajaxsettingcfn;
ajaxsettingcfn = null;
}
if (ajaxsettingcfn) {
if (ajaxsettingcfn.url) { this.url = ajaxsettingcfn.url; }
if (ajaxsettingcfn.charset) { this.charset = ajaxsettingcfn.charset; }
if (ajaxsettingcfn.data) {
if (ajaxsettingcfn.data.substr(ajaxsettingcfn.data.length - 1) == "&") {
this.data += ajaxsettingcfn.data;
}
else {
this.data += "&" + this.data;
}
this.url += this.data;
}
} var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = $C("script");
script.charset = this.charset;
script.id = "STOCKWARNSCRIPTTAG"
script.type = "text/javascript";
script.src = this.url + "&rn=" + this.getRandomNumber(); head.appendChild(script);
document.body.appendChild(script);
script.onload = script.onreadystatechange = function () {
if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {
if (callback) {
callback();
}
script.onload = script.onreadystatechange = null;
$G("STOCKWARNSCRIPTTAG").parentNode.removeChild($G("STOCKWARNSCRIPTTAG"));
}
};
}
}
html代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>股票预警测试页面</title>
<style type="text/css">
.menu-post {
background:none repeat scroll 0 0 #0673B5;
border:2px solid #A9DAF7;
padding:1px;
}
.menu {
background:none repeat scroll 0 0 #FFFFFF;
border:1px solid #A9DAF7;
position:absolute;
}
.menu-post .menu-b {
background:none repeat scroll 0 0 #F8F8F8;
border:1px solid #A9DAF7;
} .tac {
text-align:center;
}
.menu-b .h {
border-bottom:1px solid #9DB3C5;
}
.h {
background:none repeat scroll 0 0 #1395E0;
border-bottom:1px solid #9DB3C5;
color:#FFFFFF;
line-height:30px;
padding:0 10px;
text-align:left;
}.h span a, .h span {
color:#336699;
}.h span {
font-weight:normal;
}
</style>
<script src="Scripts/StockWarn.js" language="javascript" type="text/javascript"></script>-->
<script language="javascript" type="text/javascript" >
//远程URL地址:http://data.585.com/opendatahandler.ashx?action=openstockdata&source=stockquotation_only&keyword=sh600036
window.onload = function () {
var stockid = "sh600036";
AjaxHelper.getScript({
url: "http://data.585.com/opendatahandler.ashx?action=openstockdata",
data: "source=stockquotation_only&keyword=" + stockid
}, function () { alert(serverState); //这里会报错,没定义,但你打开远程URL地址是有的,重点问题是解决这个
alert("执行完毕:" + stockid); });
} </script>
</head>
<body>
<a href="javascript:;">sh600031</a>
<a href="javascript:;">测试1</a>
<a href="javascript:;">600031</a>
<a href="javascript:;" id="test222" >123</a>
</body>
</html>
if (callback) {
callback();
}
里面的callback方法;但firefox好像不行,不知道什么问题,疯掉了真TM的敲一个兼容问题就出来
$G = function (id) {
return document.getElementById(id);
};
if (typeof $C == 'undefined')
$C = function (t) {
return document.createElement(t);
};
this.data += "&" + this.data;
这个地方写错了。。哈哈,每次快刀斧提的我去调试那方面都很有收获现在IE已经彻底解决了但FireFox似乎无法触发
if (!script.readyState || script.readyState == "loaded" || this.readyState == "complete") {
无法触发这里面的方法}
应该写成 this.data += "&" + ajaxsettingcfn.data;
if( document.all ){
script.onreadystatechange = function(){
if(/(complete|loaded)/.test(this.readyState)){
callback(); script.onreadystatechange = null; script.parentNode.removeChild(script);
}};
}else{
script.onload = function(){ callback(); script.onload = null; script.parentNode.removeChild(script); };
}
}