I have a Visualforce page with a custom link that calls a function in the page's controller extension. Before that function gets called, I'd like to use JavaScript to pop up an confirmation dialog box along the lines of "You have chosen to mark this item as resolved. Your name will be added to the comments. OK/Cancel". If OK is clicked, we proceed to the Apex function call. If Cancel is clicked, nothing happens.

I've found this example:

Custom JS confirm button for IF

But it looks to me like it will always call the Apex function. My understanding of what's happening is likely incomplete.

I'm not using any JavaScript packages at present. I'd like to keep things that way if possible.

Here's the code I started with. It's a trimmed down version of what I linked to and gets accessed by adding the following to the link onclick="return showConfirmation();"

<apex:outputPanel id="sendScript">
    <script type="text/javascript">
        function showConfirmation() {
            return confirm('All selected items will be marked resolved. Your name will be added to the comments.');
  • May help if you include your relevant VF code and what you've already tried. Mar 20, 2014 at 16:29

1 Answer 1


The answer that you found is OK. You could try this:

<apex:commandLink onclick="return confirm('You have chosen to mark this item as resolved. Your name will be added to the comments. OK/Cancel');" action="{!yourMethod}" value="Call your method"/>

If you return a false onclick the method will not be called.
The confirm function returns true or false (OK or Cancel).

  • Thank you! That works and it is simpler than the example I linked to. The only thing I'm having trouble wrapping my head around is why "YourMethod" doesn't get called when "cancel" is chosen.
    – Jagular
    Mar 20, 2014 at 17:09
  • 1
    In that case, the confirm method returns a false, when the event onclick returns a false the event "propagation" stops. That even calls yourMethod from the browser to the server (were your controller is). Mar 20, 2014 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .