简体   繁体   中英

Automating javascript confirmation box

As is known, when a javascript confirmation alert/box pops up, the browser focuses on the box leaving everything else disabled until one of two options are chosen, when "OK" is clicked it returns true, when "Cancel" is clicked it returns false.

Now when writing scripts that involve confirmation alerts, my statements are based on the return of these booleans, now I am wondering (I do not know if this is a dumb question), ist here a way in javascript alone to force or automate the return of either true or false?

For instance, I write a script to detect a confirmation alert, and then force it to return true or false in my script.

Note: I am a mere beginner in javascript, so I do not know if this is a good question or not, but I would appreciate an answer.

Not from within the page.

JavaScript is single threaded and confirm and alert are blocking. No JS will run while they are waiting for input.

If you were, for instance, writing tests for this, then you could try mocking the entire confirm or alert function.

 window.confirmValue = true; window.confirm = function () { return window.confirmValue; } if (confirm("Hello, world")) { console.log("True"); } 

If you were writing JS from outside the page (eg to drive PhantomJS) then the approach might be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM