js异步加载服务端数据
时间:2010-08-24 来源:伊伦
customizeRbtServer={
httpURL: top.ucDomain+"/Support/ServiceAPI/",
requestData:function(sid,fileName,callBack){
var dataHref=customizeRbtServer.httpURL+fileName + "?sid="+sid;
var scriptId="jsReuest";
try{
scriptId=fileName.split('.')[0];
}catch(e){}
Utils.requestByScript(scriptId, dataHref, callBack)
}
}
customizeRbtUI={
GetCustomizeRbt:function(){
customizeRbtServer.requestData(top.UserData.ssoSid,"Gettjmusic.ashx",customizeRbtUI.CheckCustomizeRbt);
},
CheckCustomizeRbt:function(){
if(typeof(colorRing) =="undefined" || colorRing==null)
return;
if(colorRing["code"]=="S_OK")
{
showOrderRing(colorRing["customizeRbt"],"");
}
else
{
showOrderRing("","");
}
colorRing=null;
}
}
/**
* 添加script元素到dom中,常用来异步加载资源<br>
* <pre>示例:<br>
* <br>Utils.requestByScript(<br>
* "loadAScript",<br>
* "sample-requestByScript.js",<br>
* function(){ 运行加载脚本文件的方法... });
* </pre>
* @param {string} scriptId script标签的id,该参数其实意义不大,可以无视。
* @param {string} dataHref script标签的路径,即src属性。
* @param {function} callback script onload后的回调函数。
* @param {string} charset 标记的charset属性
* @param {Object} retry 重试的次数,间隔,如{times:5,timeout:10000},如果没加载成功,则重试5次,每次间隔10秒(不建议使用)。
* @return {无返回值}
*/
requestByScript: function(scriptId, dataHref, callback, charset, retry)
{
var isReady = false;
if (callback)
{
if (typeof (callback) == "string")
{
charset = callback;
callback = null;
}
}
var head = document.getElementsByTagName("head")[0];
var objScript = document.getElementById(scriptId);
if (objScript && !document.all)
{
objScript.src = "";
objScript.parentNode.removeChild(objScript);
objScript = null;
}
if (objScript != null)
{
if (dataHref.indexOf("?") == -1) dataHref += "?";
dataHref += "&" + Math.random();
objScript.src = dataHref;
var dataScript = objScript;
} else
{
var dataScript = document.createElement("script");
dataScript.id = scriptId;
if (charset)
{
dataScript.charset = charset;
} else
{
dataScript.charset = "GB2312";
}
dataScript.src = dataHref;
dataScript.defer = true;
dataScript.type = "text/javascript";
head.appendChild(dataScript);
}
if (document.all)
{
dataScript.onreadystatechange = function()
{
if (dataScript.readyState == "loaded" || dataScript.readyState == "complete")
{
isReady = true;
if (callback) callback();
}
}
} else
{
dataScript.onload = function()
{
isReady = true;
if (callback) callback();
}
}
if (retry)
{
setTimeout(function()
{
if (retry.times > 0 && !isReady)
{
retry.times--;
if (dataHref.indexOf("?") == -1) dataHref += "?";
dataHref += "&" + Math.random();
Utils.requestByScript(scriptId, dataHref, callback, charset, retry);
}
}, retry.timeout);
}
}
}