Skip to content

CefJSDialogHandler

Header: cef_jsdialog_handler.h
Interfaces: CefJSDialogCallback, CefJSDialogHandler
Category: Browser Handlers

Overview

User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.

CefJSDialogCallback

Source File: include/cef_jsdialog_handler.h

Process / Thread Context: Browser process / UI thread (originating OnJSDialog / OnBeforeUnloadDialog are UI thread). USER-CALLED (source=library).

Purpose: Async continuation callback for JS dialog (alert, confirm, prompt) and beforeunload requests.

Methods

void Continue(bool success, const CefString& user_input)

Parameters:

  • success: true if OK was pressed.
  • user_input: Required for prompt dialogs; may be empty for alert/confirm.

Return Value: None.

Usage Instruction: Call once when the custom dialog is dismissed.

Threading Constraint: Threading constraint not specified per-method; class comment says UI thread.

Usage Example

cpp
// After custom JS prompt dialog dismissed:
callback->Continue(ok_pressed, user_typed_text);

CefJSDialogHandler

Source File: include/cef_jsdialog_handler.h

Process / Thread Context: Browser process / UI thread. USER-IMPLEMENTED (source=client); returned from CefClient::GetJSDialogHandler().

Purpose: Handle JavaScript dialogs (alert/confirm/prompt) and the onbeforeunload leave-page dialog.

Methods

bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin_url, JSDialogType dialog_type, const CefString& message_text, const CefString& default_prompt_text, CefRefPtr<CefJSDialogCallback> callback, bool& suppress_message)

Parameters:

  • browser: The browser.
  • origin_url: Originating URL; may be empty. Can be passed to CefFormatUrlForSecurityDisplay for friendly display.
  • dialog_type: cef_jsdialog_type_t (alert/confirm/prompt).
  • message_text: The dialog message.
  • default_prompt_text: Initial value for prompt dialogs only.
  • callback: Call Continue(success, user_input) on dismissal.

Return Value: false (with suppress_message=false) = use default (one modal at a time, others suppressed). false (with suppress_message=true) = silently drop. true = custom dialog; must call callback once dismissed.

Usage Instruction: Provide a custom UI for JS dialogs.

Threading Constraint: "Called on the UI thread."

bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser, const CefString& message_text, bool is_reload, CefRefPtr<CefJSDialogCallback> callback)

Parameters:

  • browser: The browser.
  • message_text: The leave-page message.
  • is_reload: true if the user is reloading (vs navigating away).
  • callback: Call Continue(success, ...) when dismissed.

Return Value: false = default dialog; true = custom dialog (must call callback on dismissal).

Usage Instruction: Customize the "leave page?" prompt.

Threading Constraint: Threading constraint not specified explicitly per-method; class comment says UI thread.

void OnResetDialogState(CefRefPtr<CefBrowser> browser)

Parameters:

  • browser: The browser.

Return Value: None.

Usage Instruction: Cancel any pending dialogs and reset state. Called on navigation regardless of pending dialogs.

Threading Constraint: Threading constraint not specified explicitly per-method; class comment says UI thread.

void OnDialogClosed(CefRefPtr<CefBrowser> browser)

Parameters:

  • browser: The browser.

Return Value: None.

Usage Instruction: Called when the JS dialog is closed.

Threading Constraint: Threading constraint not specified explicitly per-method; class comment says UI thread.

Usage Example

cpp
class MyJSDialogHandler : public CefJSDialogHandler {
 public:
  bool OnJSDialog(CefRefPtr<CefBrowser> browser, const CefString& origin,
                  JSDialogType type, const CefString& msg,
                  const CefString& def, CefRefPtr<CefJSDialogCallback> cb,
                  bool& suppress) override {
    // Replace with native dialog; on close:
    //   cb->Continue(ok, user_input);
    return true;
  }
  bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser>, const CefString&, bool,
                            CefRefPtr<CefJSDialogCallback> cb) override { return false; }
  void OnResetDialogState(CefRefPtr<CefBrowser>) override {}
  void OnDialogClosed(CefRefPtr<CefBrowser>) override {}
  IMPLEMENT_REFCOUNTING(MyJSDialogHandler);
};

// MyClient::GetJSDialogHandler() returns this.

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.