0

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?

David Segonds
  • 83,345
  • 10
  • 45
  • 66
ader277
  • 35
  • 5
  • possible duplicate of [setting innerHTML with a script inside](http://stackoverflow.com/questions/11694977/setting-innerhtml-with-a-script-inside) – Quentin Nov 26 '12 at 19:27
  • i have tried document.getElementsByTagName("script")[4].innerHTML='var change=1 '; – ader277 Nov 26 '12 at 19:28
  • var script = document.createElement('script'); script[(script.innerText===undefined?"textContent":"innerText")] = 'change =1;'; document.body.appendChild(script); does not work – ader277 Nov 26 '12 at 19:47

2 Answers2

1

<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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • yes tried change=1 and window.change=1 and document.change=1 . – ader277 Nov 26 '12 at 19:44
  • if(change==1) { window.onbeforeunload = function (e) { var e = e || window.event; //IE & Firefox if (e) { e.returnValue = 'Are you sure?'; } // For Safari return 'Are you sure?'; }; } this fragment of code it is not executed that why i know it's not working. – ader277 Nov 26 '12 at 19:51
  • If you've already run the code, it's not going to run again just because a variable changes. You have to call it again. Or, put the `if change == 1` bit inside the function. – Niet the Dark Absol Nov 26 '12 at 19:52
0

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.

roryf
  • 29,592
  • 16
  • 81
  • 103
  • ok i did find the problem : i was doing that if(change==1) { window.onbeforeunload = function (e) { var e = e || window.event; //IE & Firefox if (e) { e.returnValue = 'Are you sure?'; } // For Safari return 'Are you sure?'; }; } but i had to do that : window.onbeforeunload = function (e) {if(change==1) { var e = e || window.event; //IE & Firefox if (e) { e.returnValue = 'Are you sure?'; } // For Safari return 'Are you sure?'; }; } – ader277 Nov 26 '12 at 19:58