hunter9000
10-03-2010, 10:40 AM
I'm having trouble getting some javascript code that I add to the page during an ajax call to run. I have some data stored in javascript variables, and when I make an ajax call, I need to set their values with the javascript code returned by the ajax call. I know it's kind of confusing, so I tried to make an example that was as simple as possible. Here's the php for test.php, the main page.
<html>
<head>
<script type='text/javascript' src='scripts/test.js' ></script>
<script type='text/javascript' >
var testVariable = 1; <!-- THE VARIABLE TO BE UPDATED -->
function ajax() {
params = "";
request = new ajaxRequest(); // create ajax object
request.open("POST", "writevars.php", true);
// set headers
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
// assign readystate change event handler
request.onreadystatechange = function() {
if (this.readyState == 4) { // completed
if (this.status == 200) { // success
if (this.responseText != null) {
<!-- WRITE THE RESPONSE TO THE PAGE INSIDE A DIV -->
document.getElementById('aDiv').innerHTML = this.responseText;
setdata(); <!-- ATTEMPT TO CALL THE NEWLY WRITTEN FUNCTION -->
alert("during ajax" + testVariable);
}
else {
alert("No data received");
}
}
else {
alert("Error " + this.statusText);
}
}
}
request.send(params);
}
</script>
</head>
<body onLoad="test()">
<div id='aDiv'></div>
<script type='text/javascript' >
function test() {
ajax(); <!-- CALL THE AJAX FUNCTION -->
alert("after ajax" + testVariable);
}
</script>
</body>
</html>
<?php
if (isset($_POST)) {
echo "<script type='text/javascript' >";
echo " function setdata() {";
echo " testVariable = 5;";
echo " }";
echo "</script>";
}
?>
I create a testVariable and give it a default value. When the page loads, I call test(), which just calls the function ajax(). ajax() makes the ajax request to writevars.php, which just writes some javascript code to set the value of testVariable inside another function, called setData().
The problem is that after I write the ajax response to the page, setData() always gives an error. My hunch is that the javascript parser only runs when the page first loads, so any code added afterwards isn't recognized by it, but I'm not sure. Is there a simpler way to update the value of a javascript variable after fetching the new value with ajax? Thanks!
<html>
<head>
<script type='text/javascript' src='scripts/test.js' ></script>
<script type='text/javascript' >
var testVariable = 1; <!-- THE VARIABLE TO BE UPDATED -->
function ajax() {
params = "";
request = new ajaxRequest(); // create ajax object
request.open("POST", "writevars.php", true);
// set headers
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
// assign readystate change event handler
request.onreadystatechange = function() {
if (this.readyState == 4) { // completed
if (this.status == 200) { // success
if (this.responseText != null) {
<!-- WRITE THE RESPONSE TO THE PAGE INSIDE A DIV -->
document.getElementById('aDiv').innerHTML = this.responseText;
setdata(); <!-- ATTEMPT TO CALL THE NEWLY WRITTEN FUNCTION -->
alert("during ajax" + testVariable);
}
else {
alert("No data received");
}
}
else {
alert("Error " + this.statusText);
}
}
}
request.send(params);
}
</script>
</head>
<body onLoad="test()">
<div id='aDiv'></div>
<script type='text/javascript' >
function test() {
ajax(); <!-- CALL THE AJAX FUNCTION -->
alert("after ajax" + testVariable);
}
</script>
</body>
</html>
<?php
if (isset($_POST)) {
echo "<script type='text/javascript' >";
echo " function setdata() {";
echo " testVariable = 5;";
echo " }";
echo "</script>";
}
?>
I create a testVariable and give it a default value. When the page loads, I call test(), which just calls the function ajax(). ajax() makes the ajax request to writevars.php, which just writes some javascript code to set the value of testVariable inside another function, called setData().
The problem is that after I write the ajax response to the page, setData() always gives an error. My hunch is that the javascript parser only runs when the page first loads, so any code added afterwards isn't recognized by it, but I'm not sure. Is there a simpler way to update the value of a javascript variable after fetching the new value with ajax? Thanks!