Select to view content in your preferred language

Function not Firing From Button

3128
5
Jump to solution
04-07-2015 11:46 AM
IanPeebles
Frequent Contributor

Good afternoon.  I have an application where I am tying a function to a button.  When the button is pressed, I want the window/application to close.  For some reason, I am getting the message that exitApplication is not defined.  Here is my simple code:

JavaScript

  // Exit Application if User does not Acknowledge

  function exitApplication() {

     window.close();

  }

HTML Portion

<div dojotype="dijit.form.Button" id="buttonExit" data-dojo-type="dijit/form/Button" type="button" style="align-content: center" onclick="exitApplication();">Close</div>

I have this working in another application (JavaScript 3.9 API).  I am now trying to get this to work using the JavaScript 3.13 API.

Anyone have any ideas?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Is that function defined outside the "require" section of the code?

require([...], function(...){
  //more code
});

function exitApplication() {
    window.close(); 
}

That's usually the issue when this error pops up. Steve Cole​'s answer would probably have the same issue, since that would also be within the require. An alternative would be

registry.byId('buttonExit').on('click', function() { 
    window.close();
});

View solution in original post

0 Kudos
5 Replies
SteveCole
Honored Contributor

I think your best bet is to add an event listener for the click event of your button:

            on(dom.byId('buttonExit'), 'click', function() {
                exitApplication();
            }); 

Since you're using a Dojo button, you could also do the same thing using dijit.byId as well:

            on(dijit.byId('buttonExit'), 'click', function() {
                exitApplication();
            }); 
0 Kudos
KenBuja
MVP Esteemed Contributor

Is that function defined outside the "require" section of the code?

require([...], function(...){
  //more code
});

function exitApplication() {
    window.close(); 
}

That's usually the issue when this error pops up. Steve Cole​'s answer would probably have the same issue, since that would also be within the require. An alternative would be

registry.byId('buttonExit').on('click', function() { 
    window.close();
});
0 Kudos
IanPeebles
Frequent Contributor

This line of code is it:

registry.byId('buttonExit').on('click', function() {  

      window.close();

      openedWindow.close();    

});

Thank you for all of your responses.

The window.close() works for I.E.  what works for closing Google Chrome and FireFox Browsers?

0 Kudos
SteveCole
Honored Contributor

It's not directly (officially) supported in Chrome or Firefox. You can read about some hackarounds here.

0 Kudos
KenBuja
MVP Esteemed Contributor

My answer was partially incorrect. If the function is inside the require statement, like shown below, then the html code will not be able to access it

require([...], function(...){  
    
    //more code
    
    function exitApplication() {  
        window.close();   
    }
});

<div dojotype="dijit.form.Button" id="buttonExit" data-dojo-type="dijit/form/Button" type="button" style="align-content: center" onclick="exitApplication();">Close</div>

When it is outside the require statement, like shown below, the event handling code won't be able to access it

require([...], function(...){  
    
    //more code
    
    registry.byId('buttonExit').on('click', exitApplication);  

});  
      
function exitApplication() {  
    window.close();   
}  
0 Kudos