PDA

View Full Version : Small Help for Ajax



pfeifer
November 21st, 2008, 11:02 AM
Hi All,

I have modified the scripting.lhtml but I need to have a periodic automatic refresh of variable value.

Can someone help me?


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<LINK href="styles.css" rel="stylesheet" type="text/css">
<TITLE>Girder 4.0 Webserver</TITLE>
//
<SCRIPT type="text/javascript">


var formRequester = null;

window.onload = init;


/* Initialisation for JS capable browsers */
function init()
{
var Data1 = document.getElementById("Data1");
Data1.onkeyup = sendvarForm;
return true;
}

/* Execute the action which has been associated with the completion of this object */
function onreadystatechangeForm()
{
var Data2 = document.getElementById("Data2");

// If XMLHR object has finished retrieving the data
if (formRequester.readyState == 4)
{
// If the data was retrieved successfully
try
{
if (formRequester.status == 200)
{
//sentForm();
Data2.value = formRequester.responseText;

}
// IE returns a status code of 0 on some occasions, so ignore this case
else if (requester.status != 0)
{
alert("There was an error while retrieving the URL: " + formRequester.statusText);
}
}
catch (error)
{
}
}

return true;
}

function sendvarForm()
{
/* Send information via XMLHR */
var Data1 = document.getElementById("Data1");

var CGIString = "Data=" + Data1.value;

try
{
formRequester = new XMLHttpRequest();
}
catch (error)
{

try
{
formRequester = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (err)
{
try
{
formRequester = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (err2)
{
formRequester = null;
return false;
}
}
}


formRequester.onreadystatechange = onreadystatechangeForm;

formRequester.open("GET", "ajaxreq.lhtml?" + CGIString);
formRequester.send(null);

return false;
}
</SCRIPT>
</HEAD>

<BODY>

<TABLE width="100%" cellpadding="10" cellspacing="10">
<TR>

<TD class="content" valign="top">

<FORM id="varform" name="varform">
Variable Name: <INPUT type="text" name="Data1" id="Data1"> (for example: _VERSION)<BR>
Result:<BR>
<TEXTAREA name="Data2" id="Data2" cols="60" rows="10">
</TEXTAREA>
</FORM>


</TD>
</TR>
</TABLE>
</BODY>
</HTML>


Thanks

Sandro

pfeifer
November 26th, 2008, 02:08 PM
up
:confused:

harleydude
November 26th, 2008, 05:11 PM
What typically would be done is to have a timer initiated when the window loads. Yours currently is

window.onload = init;
Which runs the init() method below it.

If you want to execute a ajax request say every second you will need something like

window.onload = setInterval("YourMethodHere()", 1000);
function YourMethodHere()
{
//Do something
}

pfeifer
November 26th, 2008, 11:52 PM
What typically would be done is to have a timer initiated when the window loads. Yours currently is

window.onload = init;
Which runs the init() method below it.

If you want to execute a ajax request say every second you will need something like

window.onload = setInterval("YourMethodHere()", 1000);
function YourMethodHere()
{
//Do something
}


Thanks harleydude. Work fine.

Bye

Sandro