天天看點

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

通過:

xmlhttp.onreadystatechange= function(){FuncName(param)};

or

xmlhttp.onreadystatechange= new Function("FuncName(param)");

就可以了。

//直接通過XMLHttpRequest對象擷取遠端網頁源代碼

function getSource(site,action){

if(action=="Baidu"){

xmlHttp.onreadystatechange=writeBaidu;    //設定回調函數

xmlHttp.open("POST", "../Get/js_baidu.asp", true);

}

if(action=="Google"){

xmlHttp.onreadystatechange=writeGoogle;    //設定回調函數

xmlHttp.open("POST", "../Get/js_google.asp", true);

}

if(action=="Alexa"){

xmlHttp.onreadystatechange=function(){writeAlexa(site);};    //設定回調函數

xmlHttp.open("POST", "../Get/js_alexa.asp", true);

}

if(action=="PageRank"){

xmlHttp.onreadystatechange=writePageRank;    //設定回調函數

xmlHttp.open("POST", "../Get/js_pr.asp", true);

}

xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xmlHttp.send("site=" + encodeURIComponent(site));  //發送包含位址資訊的請求體

}

//将遠端網頁源代碼寫入頁面文字區域

function writeBaidu(){

if (xmlHttp.readyState==4){

baiduSource=xmlHttp.responseText;

var someText=baiduSource;

var pattern=new RegExp(/找到相關結果數((.|\n)*?)個/);

//var outCome_exec=pattern.exec(someText);

//document.getElementById("Baidu").value=outCome_exec[1].replace(',','');

var outCome_matc=someText.match(pattern);

document.getElementById("Baidu").value=outCome_matc[1].replace(',','');

  document.getElementById("Tip_Baidu").innerHTML="

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

";

getSource("","Google");

}

}

//将遠端網頁源代碼寫入頁面文字區域

function writeGoogle(){

if (xmlHttp.readyState==4){

var someText=xmlHttp.responseText;

var pattern=new RegExp(/找到約 ((.|\n)*?) 條結果/);

//var outCome_exec=pattern.exec(someText);

//document.getElementById("Google").value=outCome_exec[1].replace(',','');

var outCome_matc=someText.match(pattern);

document.getElementById("Google").value=outCome_matc[1].replace(',','');

  document.getElementById("Tip_Google").innerHTML="

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

";

getSource("","Alexa");

}

}

//将遠端網頁源代碼寫入頁面文字區域

function writeAlexa(site){

if (xmlHttp.readyState==4){

var strRegExp="\n((.|\n)*?)"

var someText=xmlHttp.responseText;

var pattern=new RegExp(strRegExp);

//var outCome_exec=pattern.exec(someText);

//document.getElementById("Alexa").value=outCome_exec[1].replace(',','');

var outCome_matc=someText.match(pattern);

document.getElementById("Alexa").value=outCome_matc[1].replace(',','');

  document.getElementById("Tip_Alexa").innerHTML="

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

";

getSource("","PageRank");

}

}

//将遠端網頁源代碼寫入頁面文字區域

function writePageRank(){

if (xmlHttp.readyState==4){

var someText=xmlHttp.responseText;

document.getElementById("PageRank").value=someText;

  document.getElementById("Tip_PageRank").innerHTML="

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

";

writeBaidu_Date("");

}

}

//将遠端網頁源代碼寫入頁面文字區域

function writeBaidu_Date(site){

var strRegExp="  www."+site+"\/ (\\d{4}-(\\d{2}|\\d{1})-(\\d{2}|\\d{1}))  ";

var someText=baiduSource;

var pattern=new RegExp(strRegExp);

//var outCome_exec=pattern.exec(someText);

//document.getElementById("Baidu_Date").value=outCome_exec[1].replace(' ','');

var outCome_matc=someText.match(pattern);

document.getElementById("Baidu_Date").value=outCome_matc[1].replace(' ','');

 document.getElementById("Tip_Baidu_Date").innerHTML="

xmlhttp 傳遞變量消失_給XMLHttpReq.onreadystatechange傳遞參數

";

}

//根據window.XMLHttpRequest對象是否存在使用不同的建立方式

if (window.XMLHttpRequest){

xmlHttp=new XMLHttpRequest();     //FireFox、Opera等浏覽器支援的建立方式

}

else{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); //IE浏覽器支援的建立方式

}

getSource("","Baidu");