Can I avoid the "Are you sure you want to navigate away from this page?" popup?

780 views
Skip to first unread message

Jim Bourekas

unread,
Sep 27, 2013, 8:20:24 AM9/27/13
to cefs...@googlegroups.com

Hello,

When I have an open website form within CefSharp and navigate away, I get the familiar "Are you sure you want to navigate away from this page?" message. 

Is there a way to avoid this message? It currently blocks my automations until the user clicks on the popup.

Thank you in advance,

Jim

Czarek Tomczak

unread,
Sep 27, 2013, 2:54:40 PM9/27/13
to cefs...@googlegroups.com
On Friday, September 27, 2013 2:20:24 PM UTC+2, Jim Bourekas wrote:
When I have an open website form within CefSharp and navigate away, I get the familiar "Are you sure you want to navigate away from this page?" message. 

Is there a way to avoid this message? It currently blocks my automations until the user clicks on the popup.

In CEF there is the CefJSDialogHandler::OnBeforeUnloadDialog() callback:

  ///
  // Called to run a dialog asking the user if they want to leave a page. Return
  // false to use the default dialog implementation. Return true if the
  // application will use a custom dialog or if the callback has been executed
  // immediately. Custom dialogs may be either modal or modeless. If a custom
  // dialog is used the application must execute |callback| once the custom
  // dialog is dismissed.
  ///
  /*--cef(optional_param=message_text)--*/
  virtual bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
                                    const CefString& message_text,
                                    bool is_reload,
                                    CefRefPtr<CefJSDialogCallback> callback) {
    return false;
  }


Regards,
Czarek 

Jim Bourekas

unread,
Sep 28, 2013, 7:59:04 AM9/28/13
to cefs...@googlegroups.com
Thanks Czarek,

I'm not sure I understand. I am using the CEF1 implementation with the WPF dlls. Is there an option that I can use or do I have to recompile the dlls overriding the CefJSDialogHandler? I am afraid that I might break something.

Regards,

Jim

Czarek Tomczak

unread,
Sep 28, 2013, 8:13:08 AM9/28/13
to cefs...@googlegroups.com
Jim,

On Saturday, September 28, 2013 1:59:04 PM UTC+2, Jim Bourekas wrote:

I'm not sure I understand. I am using the CEF1 implementation with the WPF dlls. Is there an option that I can use or do I have to recompile the dlls overriding the CefJSDialogHandler? I am afraid that I might break something.

The link I shared with you is for CEF 3.

In CEF 1 the OnBeforeUnloadDialog() callback does not exist, but there is the OnJSConfirm() callback,
but I'm not sure if this will work for the "onunload" case, need to be tested:


I don't know if any these APIs were exposed to CefSharp, you need to check that out yourself.

-Czarek

Jim Bourekas

unread,
Sep 28, 2013, 9:35:43 AM9/28/13
to cefs...@googlegroups.com
Dera Czarek,

Why is the "onunload" responsible for this popup?

Thanks

jim

Czarek Tomczak

unread,
Sep 28, 2013, 10:22:15 AM9/28/13
to cefs...@googlegroups.com
On Saturday, September 28, 2013 3:35:43 PM UTC+2, Jim Bourekas wrote:
Why is the "onunload" responsible for this popup?

Did you try google?

-Czarek 

Jim Bourekas

unread,
Sep 28, 2013, 11:02:26 AM9/28/13
to cefs...@googlegroups.com
Ok, you have a point there. 

I just did google it and it seems that I could just override the window.onbeforeunload behaviour every time that a page was loaded. This way I would prevent the popup by returning null to the user.

Unless I could tap into the places in the code that you are referring to and override the CefSharp behaviour.

Is this about right?

Thanks again,

Jim

Czarek Tomczak

unread,
Sep 28, 2013, 11:06:53 AM9/28/13
to cefs...@googlegroups.com
On Saturday, September 28, 2013 5:02:26 PM UTC+2, Jim Bourekas wrote:
I just did google it and it seems that I could just override the window.onbeforeunload behaviour every time that a page was loaded. This way I would prevent the popup by returning null to the user.

Unless I could tap into the places in the code that you are referring to and override the CefSharp behaviour.

Yes, that's about it.

-Czarek 

Jim Bourekas

unread,
Sep 28, 2013, 11:10:52 AM9/28/13
to cefs...@googlegroups.com
Cool, thanks a lot Czarek!

jim

Jim Bourekas

unread,
Oct 17, 2013, 8:58:41 AM10/17/13
to cefs...@googlegroups.com
For anyone who may be interested, here's how I did it:

I implemented the IJsDialogHandler interface:

public bool OnJSAlert(IWebBrowser browser, string url, string message)
        {
            return false;
        }

        public unsafe bool OnJSConfirm(IWebBrowser browser, string url, string message, bool* retval)
        {
            *retval = true; //this helps to avoid the "navigation away" popup
            return true;
        }

        public unsafe bool OnJSPrompt(IWebBrowser browser, string url, string message, string defaultValue, bool* retval, ref string result)
        {
            return false;
        } 

This did the trick

Regards,

Jimb

Jim Bourekas

unread,
Oct 17, 2013, 5:16:52 PM10/17/13
to cefs...@googlegroups.com
I forgot one line: 

webviewer.JsDialogHandler = this;
Reply all
Reply to author
Forward
0 new messages