Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JavaScript engine provides three types of dialogs: Alert, Confirm and Prompt. When JavaScript engine needs to display one of these dialogs, an appropriate method of the DialogHandler is invoked:(JavaScript引擎提供三种类型的对话框:警报,确认和提示。当JavaScript引擎需要显示以下对话框之一时,将调用DialogHandler的适当方法:)

  • The DialogHandler.onAlert() method is invoked when JavaScript alert dialog should be displayed. It happens when the window.alert() JavaScript function is invoked. The dialog displays text message and OK" button. JavaScript execution will be blocked until Alert dialog is closed.(当JavaScript函数window.alert()被调用,弹出JavaScript警报对话框时,将调用DialogHandler.onAlert()方法。该对话框将显示文本消息和“OK”按钮。JavaScript执行将被阻止,直到关闭“警告”对话框。)
  • The DialogHandler.onConfirmation() method is invoked when JavaScript confirmation dialog should be displayed. It happens when the window.confirm() JavaScript function is invoked. The dialog displays text message, "OK" and "Cancel" buttons. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. JavaScript execution will be blocked until Confirm dialog is closed.(当JavaScript函数window.confirm()被调用,弹出JavaScript确认对话框时,将调用DialogHandler.onConfirmation()方法。该对话框显示文本消息,“OK”和“Cancel”按钮。如果用户单击“OK”,则该框返回true。如果用户单击“Cancel”,则该框返回false。在关闭“确认”对话框之前,将阻止JavaScript执行。)
  • The DialogHandler.onPrompt() method is invoked when JavaScript prompt dialog should be displayed. It happens when the window.prompt() JavaScript function is invoked. The dialog displays a text input field where user can enter some text, "OK" and "Cancel" buttons. To provide the new prompt text back to JavaScript you can use the PromptDialogParams.setPromptText(String) method. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. JavaScript execution will be blocked until Prompt dialog is closed.(当JavaScript函数window.prompt()被调用,弹出JavaScript提示对话框时,将调用DialogHandler.onPrompt()方法。该对话框显示一个文本输入字段,用户可以在其中输入一些文本,单击“OK”和“Cancel”按钮。你可以使用PromptDialogParams.setPromptText(String)方法为JavaScript提供新的提示文本。如果用户单击“确定”,则该框将返回输入值。如果用户单击“取消”,则该框返回null。 JavaScript的执行将被阻止,直到关闭“提示”对话框。)

By default JxBrowser doesn't display JavaScript dialogs and use silent mode where all dialogs are automatically closed emulating behavior when user clicks Cancel button on the dialog.(默认情况下,JxBrowser不显示JavaScript对话框,也不使用静默模式,在该模式下,当用户单击对话框上的“取消”按钮时,所有对话框都会自动关闭,从而模拟行为。)


To display JavaScript dialogs you can register your own implementation of the DialogHandler which displays appropriate UI dialogs. For example, the following code demonstrates how to register custom DialogHandler implementation that displays Alert dialog using Java Swing API and cancels Confirm and Prompt dialogs: (您可以通过注册自己的DialogHandler实现从而展示一个花里胡哨的对话框。例如,以下代码演示了如何注册自定义DialogHandler实现,该实现使用Java Swing API来显示Alert对话框,并设置Confirm和Prompt对话框的返回值为取消:) 以下代码测试结果为:执行JavaScript的alert函数会弹窗,confirm和prompt不弹窗,confirm返回false,prompt返回null。

browser.setDialogHandler(new DialogHandler() {
    @Override
    public void onAlert(DialogParams params) {
        String url = params.getURL();
        String title = "The page at " + url + " says:";
        String message = params.getMessage();
        JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
    }

    @Override
    public CloseStatus onConfirmation(DialogParams params) {
        return CloseStatus.CANCEL;
    }

    @Override
    public CloseStatus onPrompt(PromptDialogParams params) {
        return CloseStatus.CANCEL;
    }
    ...
});

Or you can register JxBrowser's DialogHandler implementation that already implements and displays UI dialogs:(或者,您可以注册JxBrowser的DialogHandler实现,该实现已经实现并显示UI对话框:)


Swing (Swing)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import com.teamdev.jxbr<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>owser.chromium.swing.DefaultDialogHandler;

Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.setDialogHandler(new DefaultDialogHandler(browserView));

JavaFX(JavaFX)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import com.teamdev.jxbrowser.chromium.javafx.DefaultDialogHandler;

Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.setDialogHandler(new DefaultDialogHandler(browserView));