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?

3
  • possible duplicate of setting innerHTML with a script inside Commented Nov 26, 2012 at 19:27
  • i have tried document.getElementsByTagName("script")[4].innerHTML='var change=1 '; Commented Nov 26, 2012 at 19:28
  • var script = document.createElement('script'); script[(script.innerText===undefined?"textContent":"innerText")] = 'change =1;'; document.body.appendChild(script); does not work Commented Nov 26, 2012 at 19:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

yes tried change=1 and window.change=1 and document.change=1 .
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.
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.
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.

1 Comment

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?'; }; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.