[SalesForce] Pass a string variable from controller into the Javascript function

I have a method which returns a Yes or No String depending on it's outcome.

The method is inside a standard controller extension. How do I pass the returned String into a function on the page in which the standard controller is called.

PAGE:

<apex:page extensions="CheckBtnExtension" standardController="myController" showHeader="false">

            <apex:form >
              <apex:actionFunction action="{! CheckIn }" name="checkIn" oncomplete="backToDetail(chkr)" />
            </apex:form>  
    <script>

        $(function() {
            $('#btn-check-in').click(function() {
                $.mobile.showPageLoadingMsg();
                checkIn();
            });         
        });       

        function backToDetail(chkr){
            alert(chkr)
        }  
     </script>

EXTENSION:

    public String CheckIn() {

    if (!recordList.isEmpty()) {        
        myObj record = recordList[0];
        isCheckedIn = record.Checked_In__c;

        if (isCheckedIn) {
            record.Checked_In__c = false;
            chkr = 'Yes'; 
        } else {
            record.Checked_In__c = true;
            chkr = 'No';
        }
        UPSERT record;
    } else {
        isCheckedIn = null;
    }  
   return(chkr);  
}

chkr is the String I want to pass into my JS. I just need to be able to stick it into an alert for now.

Any help would be appreciated.

Best Answer

You can change up some of your visualforce and javascript slightly to achieve this.

First lets change up the JS a tiny bit so it directly references the checkIn apex method and is wrapped in an element you can rerender.

<apex:outputPanel id="scriptContainer">
    <script>
        //on dom ready code is fine
        function backToDetail() {
            alert('{!checkIn}');
        }            
    </script>
</apex:outputPanel>

Now just change up the actionFunction definition so it doesnt call checkIn and instead it rerenders the script tag containing the backToDetail method. The purpose of doing this is that when it is doing the rerender it will replace alert('{!checkIn}') with alert('Yes') or alert('No').

<apex:actionFunction name="checkIn" oncomplete="backToDetail();" rerender="scriptContainer" />