Ajax transmits parameters to XMLHttpReq. onreadystatechange
Pass:
Xmlhttp. onreadystatechange = function () {xx (123 )};
Or xmlhttp. onreadystatechange = new Function ("xx (123.
Copy codeThe Code is as follows:
Ajax transmits parameters to XMLHttpReq. onreadystatechange
Pass:
Xmlhttp. onreadystatechange = function () {xx (123 )};
Or
Xmlhttp. onreadystatechange = new Function ("xx (123 )");
You can.
M = document. getElementsByName ("text8 ");
V = m [I];
XMLHttpReq. onreadystatechange = function () {proce (v )};
----------------------------------------------
Function proce (v)
{
If (XMLHttpReq. readyState = 4)
{
If (XMLHttpReq. status = 200)
{
Var res = XMLHttpReq. responseXML. getElementsByTagName ("content") [0]. firstChild. data;
V. value = res;
}
Else
{
V. value = '....';
}
}
}
An example of a small test:
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test Ajax </title>
<Mce: script language = "javascript"> <! --
Function getXMLHttpRequest (){
Var xmlHttpRequest;
Try
{
XmlHttpRequest = new XMLHttpRequest ();
}
Catch (e)
{
Try
{
XmlHttpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e)
{
Try
{
XmlHttpRequest = new ActiveXObjec ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Throw new Error ('unable to create XMLHttpRequest .')
}
}
}
Return xmlHttpRequest;
}
/*
Function test (){
Var xhr = getXMLHttpRequest ();
Var url = "http: // 211.87.235.108: 5000/sensor. xml ";
Xhr. open ("GET", url );
Xhr. send (null );
Xhr. onreadystatechange = function (){
If (4 = xhr. readyState)
{
If (200 = xhr. status ){
// Var xmldoc = xmlHttpRequest. responseXML;
Var xmldoc = xhr. responseText;
Document. getElementById ("disp"). innerHTML = xmldoc;
}
Else {
// Alert (xhr. status );
}
}
};
Xhr. onstatechange = function (){
HandleXMLHttpRequest (xhr );
};
}
*/
Function handleXMLHttpRequest (xmlHttpRequest ){
If (4 = xmlHttpRequest. readyState)
{
If (200 = xmlHttpRequest. status ){
// Var xmldoc = xmlHttpRequest. responseXML;
Var xmldoc = xmlHttpRequest. responseText;
Document. getElementById ("disp"). innerHTML = xmldoc;
}
Else {
Alert (xmlHttpRequest. status );
}
}
}
Function doXMLHttpRequest (){
Var xhr = getXMLHttpRequest ();
Var url = "http: // localhost: 5000/sensor. xml ";
Xhr. open ("GET", url );
Xhr. send (null );
Xhr. onreadystatechange = function (){
HandleXMLHttpRequest (xhr );
};
}
Function myrefresh (){
Window. location. reload ();
}
SetTimeout ('myrefresh () ', 1000); // refresh once per second
// --> </Mce: script>
</Head>
<Mce: style> <! --
Body {font-size: 12px ;}
--> </Mce: style> <style mce_bogus = "1"> body {font-size: 12px ;}</style>
<Body onLoad = "doXMLHttpRequest ()">
<Div id = "disp">
</Div>
</Body>
</Html>
Case 2:
Copy codeThe Code is as follows:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> My Documents </title>
<Mce: script language = "javascript"> <! --
Function getXMLHttpRequest (){
Var xmlHttpRequest;
Try
{
XmlHttpRequest = new XMLHttpRequest ();
}
Catch (e)
{
Try
{
XmlHttpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e)
{
Try
{
XmlHttpRequest = new ActiveXObjec ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Throw new Error ('unable to create XMLHttpRequest .')
}
}
}
Return xmlHttpRequest;
}
Function test (){
Var xhr = getXMLHttpRequest ();
Var url = "http: // 211.87.235.108: 5000/sensor. xml ";
Xhr. open ("GET", url );
Xhr. send (null );
Xhr. onreadystatechange = function (){
If (4 = xhr. readyState)
{
If (200 = xhr. status ){
// Var xmldoc = xmlHttpRequest. responseXML;
Var xmldoc = xhr. responseText;
Document. getElementById ("disp"). innerHTML = xmldoc;
}
Else {
// Alert (xhr. status );
}
}
};
}
/*
Function handleXMLHttpRequest (xmlHttpRequest ){
Alert (xmlhttpRequest + 2 );
If (4 = xmlHttpRequest. readyState)
{
If (200 = xmlHttpRequest. status ){
// Var xmldoc = xmlHttpRequest. responseXML;
Var xmldoc = xmlHttpRequest. responseText;
Document. getElementById ("disp"). innerHTML = xmldoc;
}
Else {
Alert (xmlHttpRequest. status );
}
}
}
Function doXMLHttpRequest (){
Var xhr = getXMLHttpRequest ();
Var url = "http: // 211.87.235.108: 5000/sensor. xml ";
Xhr. open ("GET", url );
Xhr. send (null );
Xhr. onreadystatechange = handleXMLHttpRequest ();
}
*/
Function myrefresh (){
Window. location. reload ();
}
// SetTimeout ('myrefresh () ', 1000); // refresh once per second
// --> </Mce: script>
</HEAD>
<Mce: style> <! --
Body {font-size: 12px ;}
--> </Mce: style> <style mce_bogus = "1"> body {font-size: 12px ;}</style>
<Body onLoad = "test ()">
<Div id = "disp">
</Div>
</Body>
</Html>