1

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

1 Answer 1

3

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;
    }
}
1
  • You likely only want to return false - canceling the form post - when the user replies that they want to cancel. Otherwise, do not execute a return statement at all. The reason you implement it like this is because there may be Salesforce-appended JS that is executed onclick which still needs to be executed after confirmation - like a rerender.
    – Mark Pond
    Commented Aug 27, 2014 at 19:27

You must log in to answer this question.