Skip to main content
dublove
Legend
August 31, 2025
Answered

How can I make if (!confirm(‘Please confirm!') only affect certain functions in main()?

  • August 31, 2025
  • 1 reply
  • 223 views

When using  if (!confirm('Please confirm!'),

Only A1(); and A2(); require confirmation.

A3() no need confirm.
Do I need to move  A3();  outside of  main();?

 

function main(){
    if (!confirm('Please confirm!'))
        return;
A1();
A2();

A3();
}

 

Correct answer m1b

@dublove do you know why it didn't work? Maybe because you wanted to share variables, but your functions were out of scope? Try putting the functions inside, and at the end of the main function. Like this:

function main() {

    if (confirm('Please confirm')) {
        A1();
        A2();
    }

    A3();

    // script finished

    function A1() {
        // do something here
    };

    function A2() {
        // do something here
    };

    function A3() {
        // do something here
    };

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

1 reply

brian_p_dts
Community Expert
Community Expert
August 31, 2025

Usually better and more logical to think through what happens in the True case. 

if(confirm("Please confirm") {

A1();

A2(),

}

A3();

dublove
dubloveAuthor
Legend
September 1, 2025

This has already been tried.
It didn't work.

Because confirm is returned to main(),

anything within `main` will be affected.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
September 1, 2025

@dublove do you know why it didn't work? Maybe because you wanted to share variables, but your functions were out of scope? Try putting the functions inside, and at the end of the main function. Like this:

function main() {

    if (confirm('Please confirm')) {
        A1();
        A2();
    }

    A3();

    // script finished

    function A1() {
        // do something here
    };

    function A2() {
        // do something here
    };

    function A3() {
        // do something here
    };

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');