5

I am trying to avoid js alert on a page as it breaks the flow and the browser is stuck on that page until the popup is clicked.

I added Class as shown on sample:

public class JsDialogHandler : IJsDialogHandler
{
    public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
    {
        return true;
    }

    public bool OnJSBeforeUnload(IWebBrowser browserControl, IBrowser browser, string message, bool isReload, IJsDialogCallback callback)
    {
        return true;
    }

    public void OnResetDialogState(IWebBrowser browserControl, IBrowser browser)
    {

    }

    public void OnDialogClosed(IWebBrowser browserControl, IBrowser browser)
    {

    }
}

And i assign to the Chromium browser as:

  CefSharp.Cef.Initialize(new CefSharp.CefSettings());

                browser = new CefSharp.WinForms.ChromiumWebBrowser(CustomLinks[0].ToString());

                JsDialogHandler jss = new JsDialogHandler();
                browser.JsDialogHandler = jss;

The thing is when alert is supposed to show it does run the OnJSDialog event. But then the browser turns white and is just stuck and trying to find a way around but not much is available online..

Any suggestions?

3
  • You need to execute the callback, read the xml doc for instructions.
    – amaitland
    Jan 19, 2017 at 21:01
  • @amaitland can you please post link for that. Jan 20, 2017 at 11:02
  • Just navigate to the method in visual studio and read the documentation
    – amaitland
    Jan 20, 2017 at 11:48

2 Answers 2

8

In the OnJSDialog method of your handler, make sure that you call Continue(...) on the callback:

public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage) {
  callback.Continue(true);
  return true;
}
0
1

If you simply want to disable javascript alerts, there is parameter suppressMessage just for that:

// Implementation of IJsDialogHandler
public bool OnJSDialog(IWebBrowser browserControl, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
{
    suppressMessage = true;
    return false;
}
// All other methods should do nothing or return false.
1
  • 1
    The best answer IMO.
    – Zintom
    Jan 5, 2021 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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