[SalesForce] How to call the Boolean variable from apex class to vf page

Could you please help me on getting the value of the boolean variable from apex controller method to VF Page.
First i will tell my requirement, If the user unable to update the record "eruObj" he should get a pop up saying "Sorry! you dont have permission to update this record"

Below is my apex controller method, Here i have set the boolean variable "NoPermission=true" in the catch black.

    public PageReference saveMethod(){
    try{
    update eruObj;
    }
    catch(Exception e){
    NoPermission=true;
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'ERRRRORRR'));
    }

}

On VF page:

<script>
function doAlert(yesno){
ForAlert();
if (yesno)
alert('Sorry! You dont have permission to update this record');

}
</script>

<apex:outputPanel id="tstpopup">
<apex:outputPanel styleClass="popupBackround" layout="block" rerender="{!displayPopUp}"/>
<apex:outputPanel styleClass="custPopup" layout="block" rerender="{!displayPopUp}"/>
<table>
<tr>
<td>
<center>
<apex:commandButton value="Save" onComplete="doAlert({!NoPermission})" rerender="tstpopup"/>
<apex:commandButton value="Cancel" action="{ClosePopup}" rerender="tstpopup"/>
</center>
</td>
</tr>
</table>
</apex:outputPanel>
</apex:outputPanel>

<apex:actionFunction name="ForAlert" action="{!saveMethod}"/>

I am not getting the pop up in the first attepmt, but its working fine from the second attempt.
It is because the value which i have assigned in the catch block is not passing in the vf page. so, at the first attempt the boolean varialbe is set to False, From the second attempt it is setting as True.

can you please help me on this,

Thanks in Advance!

Best Answer

Have a look at below fixed VF code. I feel This will resolve your issue:

<script>
var yesno=false;
function doAlert(){
if (yesno)
alert('Sorry! You dont have permission to update this record');
}
</script>

<apex:outputPanel id="tstpopup">
<Script>
yesno={!NoPermission};
</Script>
<apex:outputPanel styleClass="popupBackround" layout="block" rerender="{!displayPopUp}"/>
<apex:outputPanel styleClass="custPopup" layout="block" rerender="{!displayPopUp}"/>
<table>
<tr>
<td>
<center>
<apex:commandButton value="Save" action="{!saveMethod}" onComplete="doAlert()" rerender="tstpopup"/>
<apex:commandButton value="Cancel" action="{ClosePopup}" rerender="tstpopup"/>
</center>
</td>
</tr>
</table>
</apex:outputPanel>
</apex:outputPanel>