-2

A php page has a jquery interface. an on submit button dynamically writes an alert. the confirm box is of the style : return confirm.

in other words there is a script on the page that does something like this:

confirm (okay cancel );

I need a way of listening for when the alert has been dynamically written so I can confirm it dynamically. any ideas?

is there an event listener for 'confirm'?


    <html>
       <head>   
<script>           
                function getConfirmation() {
                   var retVal = confirm("Do you want to continue ?");
                   if( retVal == true ) {
                      document.write ("User wants to continue!");
                      return true;
                   } else {
                      document.write ("User does not want to continue!");
                      return false;
                   }
                }
           
          </script>     
       
       </head>
       
       <body>
          <p>Click the following button to see the result: </p>      
          <form>
             <input type = "button" value = "Click Me" onclick = "getConfirmation();" />
          </form>      
       </body>
    </html>
John
  • 346
  • 1
  • 9
  • 19
  • Is this your code that triggers the alert? Explain your use case in more detail. There is no event emitted by an alert built in but possibly ways of handling your situation depending on use case – charlietfl Apr 13 '19 at 14:58
  • No : the submit button prior to this triggers the alert. however I need to dynamically click [okay] when the alert pops up. @charlietfl – John Apr 13 '19 at 15:08
  • Now that it's related to a form submit the issue gets even more complex depending on what happens in the submit handlerwhat you are trying to accomplish and you really need to explain more and show a [mcve] – charlietfl Apr 13 '19 at 15:11
  • 2
    This really sounds like an [XY Problem](http://xyproblem.info/) – charlietfl Apr 13 '19 at 15:12
  • Possible duplicate of [How can I press the alert ok button programmatically?](https://stackoverflow.com/questions/10749959/how-can-i-press-the-alert-ok-button-programmatically) – Davide Vitali Apr 13 '19 at 15:22
  • 1
    @Davide Vitali : that answer has to do with Selenium IDE which is now deprecated in any case. SO no, not a duplicate. – John Apr 13 '19 at 15:38
  • 3
    An `alert()` is not the same as a `confirm()` - and no, you can't access either of these via javascript as alert/confirm halt the javascript thread while they are displayed and javascript is single-threaded. – freedomn-m Apr 13 '19 at 15:38
  • Really just back to this being an XY Problem and you haven't provided a detailed explanation of use case and what you need to accompish at a higher level – charlietfl Apr 13 '19 at 15:41
  • 2
    Please explain what you want to happen. You don't want the user to see or answer to the `confirm` dialog? Then why do you have it in the first place? Just don't trigger it... – trincot Apr 13 '19 at 15:54

1 Answers1

0

You can use window.confirm = function() { } to intercept the confirm command.

Returning true would be the same as clicking the "ok" button.

window.confirm = function() { return true; }

if (window.confirm("Delete everything?") == true)
{
    alert("Deleting all your files as you confirmed")
}
freedomn-m
  • 27,664
  • 8
  • 35
  • 57