Reputation: 35
well i have this javascript code :
<div id="change">
<script>
var change=0;
</script>
</div>
and I use ajax to update it. In fact when I update my database I want to alter the variable 'change' to the value 1.:
function update(value,username,competency)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("updateAll").innerHTML=xmlhttp.responseText;
document.getElementById("change").innerHTML='<script >var change=1</script> ';
}
};
xmlhttp.open("GET","Update_evaluation_ajax.php?value="+value+"&username="+username+"&competency="+competency,true);
xmlhttp.send(null);
}
Can you help me figure out why this does not work?
Upvotes: 0
Views: 1027
Reputation: 30170
When your initial <script>
tag is executed, it creates a global variable change
that can be accessed via window.change
.
Presumably you're expecting to use the change
variable elsewhere in your code. Without seeing that code it's difficult to suggest a good solution, but you can simply set window.change
inside you response function and any subsequent code that's executed will use the updated value (depending on scope):
function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("updateAll").innerHTML = xmlhttp.responseText;
window.change = 1;
}
};
Of course global variables are bad, but that's outside the scope of this answer.
Upvotes: 0
Reputation: 324790
<script>
tags added with innerHTML
are NOT executed.
Why not just set change = 1
instead of setting the innerHTML
? It looks like change
is a global variable, so you can change it from anywhere.
Upvotes: 1