DWQA QuestionsCategory: Development ToolHow does JavaScript know if the browser has disabled confirm
Hey。 asked 1 month ago

When calling confirm of browser, pop up confirmation box and choose to disable it. When the code calls confirm method again, it will return false. Can JS know that confirm of browser has been disabled?
How does JavaScript know if the browser has disabled confirm

2 Answers
Best Answer
ezmo answered 1 month ago

I didn’t think of a beautiful solution.
There’s a clever way: if the user chooses to disable it, the code will not be blocked when the method is called, and the next code will be executed immediately. We can set up a timer, the time is at the end of milliseconds, which means that the client has blocked the bullet window.

var begin = Date.now();

// If the client does not disable confirm, the pop-up window blocks the code from executing until the user clicks to confirm or cancel.
// Then the value between end and start will be larger.
// Conversely, the value between end and start is very small, in milliseconds.
var result = window.confirm('hello'); 

var end = Date.now();
if (end - begin < 10) {
    Console. log ('user disabled confirm window');
}
Enifenan replied 1 month ago

It is recommended to use Date. now (), mainly because this is the addition and subtraction of long and no new object is needed. Ha ha ha

michael_cai answered 1 month ago

No way to know.
It is suggested to write dialog boxes instead.
Never Use a Warning When you Mean Undo