How to Run Acrobat JavaScript Only When User Selects Yes
Question details
The user needs to configure a PDF form button to execute subsequent JavaScript commands only when the user clicks 'Yes' in a confirmation dialog box.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Adding a Yes-or-No confirmation prompt to an interactive PDF button before proceeding with document actions like 'Save As'.
- Observed behavior
- If commands are placed in separate action sequences, they run regardless of the user's choice in the alert. The execution needs to halt completely if 'No' is selected or the dialog is closed.
Ensure you have access to the Prepare Form toolset in your PDF editor and identify the specific interactive button where the confirmation dialog needs to be applied.
Combine All Actions into a Single Conditional Script
By placing the alert and all subsequent commands within a single JavaScript block, you can evaluate the user's response and prevent unwanted actions from executing.
A common mistake is assigning a JavaScript alert to a button and then adding a separate 'Execute Menu Item' action. The PDF software executes all listed actions sequentially, meaning the menu item will run even if the user clicks 'No'. To fix this, everything must be consolidated into one JavaScript action.
Enter Prepare Form mode in your PDF editor, right-click the target interactive button, and select Properties.
Navigate to the Actions tab and remove any existing non-JavaScript actions (such as 'Execute Menu Item') that are currently attached to the button.
Select 'Run a JavaScript' from the 'Select Action' dropdown menu and click the Add button.
In the JavaScript Editor, wrap your remaining actions inside an if statement that checks for a return value of 4. For example: if (app.alert({cMsg: 'Continue?', cTitle: 'Confirmation', nType: 2}) === 4) { app.execMenuItem('SaveAs'); }
Click OK to save the script, close the Properties dialog box, exit form editing mode, and click the button to test your conditional execution.

Looking for a Lightweight PDF and Office Suite?
While advanced script debugging is specific to Adobe Acrobat, WPS Office provides a comprehensive, lightweight, and free alternative for managing all your document needs. From editing standard PDF forms to creating Word documents and Excel spreadsheets, WPS Office offers seamless format compatibility and a familiar interface.
- 1. Download and Install: Visit the official WPS Office website to download the free application for your operating system.
- 2. Open Your Documents: Launch WPS Office and easily open your existing PDF, DOCX, XLSX, and PPTX files without losing any formatting.
- 3. Edit and Manage PDFs: Use the dedicated PDF tab to fill out forms, add annotations, or convert PDFs to other editable formats.

Frequently Asked Questions
Why do my other button actions run even if I click No in the alert?
The software executes all actions listed in the button's properties sequentially. A JavaScript alert cannot stop a separate, non-JavaScript action (like an Execute Menu Item command) from running. You must combine them into a single script.
What is the correct syntax to show a Yes/No dialog in PDF JavaScript?
You can create a Yes/No dialog using the app.alert method with the parameter nType: 2 (or 'YesNo'). For example: app.alert({cMsg: 'Message', nType: 2}).
How do I trigger a Save As dialog using Acrobat JavaScript?
You can trigger the Save As dialog within your script by executing app.execMenuItem('SaveAs');. Ensure this command is placed inside your conditional block so it only runs when the user confirms.




