[SalesForce] How to pass String parameter to Javascript confirm() function

How could I pass to confirm() function the value of a String contained in the Controller of this VF page?

<apex:page controller="TestJS" >

<script language="javascript">

function askForConfirmation()
{
            var result = confirm(test);
            if(result) 
            {
                return true;
            }
            return false;
        }

</script>

<apex:Form >
    <apex:commandButton value="Confermare" onclick="return askForConfirmation();">

    </apex:commandButton>
</apex:form> 

public with sharing class TestJS {
    String test='Sure?';
}

Best Answer

Try :

VF :

<script language="javascript">
    function askForConfirmation() {
            var test = '{!test}';
            var result = confirm(test);
            if(result)  {
                return true;
            }
            return false;
        }
</script>

<apex:form>
    <apex:commandButton value="Confermare" 
        onclick="if (!askForConfirmation()) { return false; }">
    </apex:commandButton>
</apex:form> 

Controller :

public with sharing class TestJS {
    String test='Sure?';

    public String getTest() {
        return test;
    }
    public void setTest(String t) {
        test = t;
    }
}
Related Topic