[SalesForce] Javascript function gets incorrect boolean value from controller

When my javascript function recieves a value from a controller boolean, it gets "false" instead of "true". Meanwhile, a System.log debug states that the boolean is "true". What could be causing this? There is much more code inside of the apex controller than I show below, so I'm thinking that possibly the javascript runs before the apex function is finished.

Here is the gist of the code:

VisualPage and Javascript

 <apex:actionSupport event="onchange" action="{!myAction}" reRender="something" oncomplete="myFunction();" />

 <script type="text/javascript">
 function myFunction(){
   window.alert({!myBoolean});
 }
 </script>

Apex Controller

public void myAction(){
  myBoolean = true;
  System.debug(myBoolean);
}

Best Answer

When the page loads, the {!myBoolean} expression gets the value of myBoolean at the time of the page load. So essentially the javascript function in your page would be:

function myFunction(){
   window.alert(false);
}

If the value of myBoolean is changed in the controller, you would also have to rerender the javascript function, or else it would remain as alert(false);

Moving the javascript function inside the something block should solve your problem.