Skip to main content
Inspiring
August 30, 2018
Answered

How do I stop running a script when I hit cancel on dialog box

  • August 30, 2018
  • 3 replies
  • 3916 views

I'm trying to set up a script that will run a function if the user selects OK from the dialog box, but not if they hit cancel. The thing is the script runs regardless. So my question is how do I stop the script when they select cancel, or run the script when the hit ok?

var buttonCancel = g.add('button', undefined, 'Cancel');

var buttonOk = g.add('button', undefined, 'Ok');

This topic has been closed for replies.
Correct answer Kukurykus

g = new Window('dialog')

 

buttonCancel = g.add('button', undefined, 'Cancel')

buttonOk = g.add('button', undefined, 'Ok')

buttonOk.onClick = function() {alert()}

buttonCancel.onClick  = function() {g.close()}

g.center(), g.show()

3 replies

Inspiring
August 31, 2018

var dialogResponse; // init variable

function mainScript() {

    showDialog();

    if (dialogResponse == "cancel") return;

    //... otherwise, do some stuff

}

function showDialog() {

    var dlg = new Window('dialog');

   

    buttonOk = dlg.add('button', undefined, 'Ok') 

    buttonOk.onClick = function(){

        dlg.close();

        dialogResponse = "ok"

    } 

    dlg.cancelBtn = dlg.add('button',undefined,'Cancel');

    dlg.cancelBtn.onClick = function(){

        dlg.close();

        dialogResponse = "cancel"

    }

   

    g.center(), g.show();

}

Chuck Uebele
Community Expert
August 31, 2018

If you want to cancel the script when you hit the cancel button, put your code either in a function or an if clause that checks to see if the cancel button was pressed (have a onClick function set a variable, such as "run" to false to do this.

var run = true

var dlg = new Window('dialog')

dlg.cancelBtn = dlg.add('button',undefined,'Cancel')

dlg.cancelBtn.onClick = function(){run = false}

if(run){

    //your code

    }

Kukurykus
KukurykusCorrect answer
Brainiac
August 30, 2018

g = new Window('dialog')

 

buttonCancel = g.add('button', undefined, 'Cancel')

buttonOk = g.add('button', undefined, 'Ok')

buttonOk.onClick = function() {alert()}

buttonCancel.onClick  = function() {g.close()}

g.center(), g.show()