1
我更喜歡不使用jQuery(儘管我知道這個文件相當大,每次頁面加載時它都會「吃」很多帶寬)。我檢查了我的代碼(如下),XMLHttpRequest被正確寫入,並且不是跨域的。在Chrome上工作正常,但在IE上,如果只有一個請求正在運行,那麼沒有問題,但下面的代碼使用2個XMLHttpRequest實例,並以某種方式訪問我的CGI,但總是返回空字符串。 我試過也發送(空),發送(「」)或發送('')。許多js XMLHttpRequest同時運行導致其中一個返回空字符串?
我的代碼:
<script type="text/javascript">
var SMR;
var UPR;
var F;
var G;
function MUPD() {
F = window.setInterval(Updator, 5000);
G = window.setInterval(Listener, 5000);
if(typeof XMLHttpRequest !== 'undefined') { SMR = new XMLHttpRequest(); UPR = new XMLHttpRequest(); }
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"]
for(var i = 0, len = versions.length; i < len; i++) {
try {
SMR = new ActiveXObject(versions[i]);
UPR = new ActiveXObject(versions[i]);
break;
}
catch(e){}
}
}
}
function Updator() {
if (document.cookie.length > 0) {
var begin = document.cookie.indexOf("TCPID=");
if (begin != -1) {
var end = document.cookie.indexOf(";", begin+6);
if (end == -1) end = document.cookie.length;
var fupd=UPD(unescape(document.cookie.substring(begin+6, end))).split("-");
if(fupd[0]=="OK") {
for(var i=1 ; i<fupd.length ; i+=2) {
var elm=document.getElementByID("I"+fupd[i]);
elm.setAttribute("src", "/theclub/cgi-bin/personalimage.cgi?UID="+fupd[i]);
if(fupd[i+1].toString()!=="OFF") { document.getElementById("T"+fupd[i]).innerHTML=fupd[i+1]; }
}
}
}
else { window.top.location.href="/theclub/login.htm?MOD=NEW"; }
}
}
function UPD(cvl) {
try {
UPR.open("GET", "/theclub/cgi-bin/chkpubupd.cgi?CVL="+cvl, false);
UPR.setRequestHeader("Content-type", "text/x-www-form-urlencoded");
UPR.send('');
if (UPR.status == 200) { return UPR.responseText; }
else { alert("Error: "+UPR.status+" >> "+UPR.responseText); return ""; }
}
catch (e) { alert("Error in updating system"); return ""; }
}
function PWR(cvl) {
try {
SMR.open("GET", "/theclub/cgi-bin/getmessages.cgi?CVL="+cvl, false);
SMR.send('');
if (SMR.status == 200 && SMR.responseText!=="") { alert(SMR.responseText);}
else if(SMR.status != 200) { alert(SMR.status+" >> "+SMR.responseText); }
}
catch (e) { alert("Error receiving messages"); }
}
function Listener() {
if (document.cookie.length > 0) {
var begin = document.cookie.indexOf("TCPID=");
if (begin != -1) {
var end = document.cookie.indexOf(";", begin+6);
if (end == -1) end = document.cookie.length;
PWR(unescape(document.cookie.substring(begin+6, end)));
}
else { window.top.location.href="/theclub/login.htm?MOD=NEW"; }
}
}
...
</script>
使UPR成爲一個私人而不是全球性的。 – dandavis
http://stackoverflow.com/questions/20771400/script-tag-vs-script-type-text-javascript-tag大概使用