开发者

Is it possible to simulate a blocked dialog like 'window.confirm()'? [duplicate]

This question already has answers here: How to create a custom "confirm" & pause js execution until user clicks button? (6 answers) Closed 6 years ago.

I want to simulate a block dialog like window.alert(), window.confirm(), or window.prompt() with DIV+JavaScript. It seems easy using a callback function. But I want to block the process while the confirm dialog is shown.

That is to say, I want to define a function like:

var开发者_JAVA百科 Alert = function(){ balabala };

which returns true or false after I click OK or Cancel.

EDIT:

For now I defined a function called Confirm() and now I have to call it like

Confirm(*callback*);

and in the implementation I show a dialog like confirm and when OK is clicked the callback will be executed. I wonder whether it is possible to rewrite it so that I can call it like

if(Confirm()) {
    callback(); 
} else {
    balabala;
}


You can't block the user from using their browser IE switching tabs and doing other things on them before they come back to yours, like an alert() call does. But you can block them from using anything on your page until they answer the question. You can just fill the entire body with an absolute positioned div that has a z-index greater than the rest of your page, and give your popup a z-index one higher than that and center it.

Of course a user can still use things like Chrome developer tools or Firebug to remove your blocking div, so it's not a secure thing.

Edit I misread your question. You don't care about blocking the page visually but are wanting to make the popup's return value synchronous with the rest of your script instead of asynchronous with a callback. I'm not sure how to go about that.

Maybe this will be of use to you: https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete


Unfortunately, there's no way to simulate synchronous code calls with just JavaScript. You can create pure DOM-based dialogs, but in order to have code execute when they close, you'd need to have it accept a callback or return a Promise. Unfortunately, this doesn't make for the most clean code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜