If your goal is to block the execution until the dilog is closed, then you should use executeAsyncScript instead of executeScript.
Here is an example to display a confirm dialog and wait for someone to close it:
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.get("http://stackoverflow.com");
Boolean confirm = (Boolean)((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[0]; setTimeout(function(){ " +
" callback(window.confirm('Are you ready?')); " +
"}, 1);");
And another one to display a prompt dialog and wait for someone to type some text and close it:
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.get("http://stackoverflow.com");
// display a confirm dialog and waits for someone to type some text
String prompt = (String)((JavascriptExecutor)driver).executeAsyncScript(
"var callback = arguments[0]; setTimeout(function(){ " +
" callback(window.prompt('Give me some text!')); " +
"}, 1);");
// display the text typed by the user
System.out.println(prompt);