4

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 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;
}
1
  • 2
    if you can block the whole process I'd report that as a bug to the browser creator. The only thing you should be able to block from javascript is the javascript execution in that single tab. Jul 24, 2011 at 9:41

2 Answers 2

1

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

1
  • yep. For detail 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 clicked callback will be excuted. I wonder whether it is possible to rewrite it so that I can call it like "if(Confirm()) callback(); else balabala"
    – zidianqs
    Jul 24, 2011 at 9:31
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.