Page 1 of 1

How to handle beforeunload ?

Posted: Sun Nov 07, 2021 8:41 pm
by thefunkyjoint
Hi,

Some websites require you to confirm before exit or load another page. I'm trying to get rid of these type of confirmation dialogs like these :
Screen Shot 2021-11-07 at 17.36.37.png
Screen Shot 2021-11-07 at 17.40.22.png
So here is what i'm doing :

Code: Select all

procedure TChromium1.onBeforeUnloadDialog(Sender: TObject; const browser: ICefBrowser; const messageText: ustring;
  isReload: Boolean; const callback: ICefJsDialogCallback; out Result: Boolean);
begin
Result := true;
end;
The code above makes the confirmation dialog not appear, but it won't let me load another page ; the browser stays on the page that shows the confirmation.

You can simulate this on any Facebook page :

1 - Navigate to www.Facebook.com and login
2 - Simulate a post on your own page , just write something but do not click on Publish.
3 - Now try to navigate to another page ; Facebook will show a confirmation message and this is the kind i want to supress ; i want the user to be able to naviage wherever he wants without any kind of confirmation.

I also tried to run this code after the page loads :

Code: Select all

window.onbeforeunload = null;
But no lucky, Facebook stills require confirmation to leave it and navigate to another page.

Re: How to handle beforeunload ?

Posted: Mon Nov 08, 2021 8:00 am
by salvadordf
Hi,

You also need to call callback.cont like this :

Code: Select all

procedure TMiniBrowserFrm.Chromium1BeforeUnloadDialog(Sender: TObject;
  const browser: ICefBrowser; const messageText: ustring;
  isReload: Boolean; const callback: ICefJsDialogCallback;
  out Result: Boolean);
begin
  Result := True;
  callback.cont(true, '');
end;
Tested on this page :
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onbeforeunload

Re: How to handle beforeunload ?

Posted: Mon Nov 08, 2021 8:04 am
by salvadordf
In case you want to replace those dialogs instead of avoid them these are the code comments for that event :

Code: Select all

  ///
  // Called to run a dialog asking the user if they want to leave a page. Return
  // false (0) to use the default dialog implementation. Return true (1) 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.
  ///
The callback.cont method has these code comments :

Code: Select all

  ///
  // Continue the JS dialog request. Set |success| to true (1) if the OK button
  // was pressed. The |user_input| value should be specified for prompt dialogs.
  ///

Re: How to handle beforeunload ?

Posted: Mon Nov 08, 2021 10:52 am
by thefunkyjoint
salvadordf wrote: Mon Nov 08, 2021 8:00 am Hi,

You also need to call callback.cont like this :

Code: Select all

procedure TMiniBrowserFrm.Chromium1BeforeUnloadDialog(Sender: TObject;
  const browser: ICefBrowser; const messageText: ustring;
  isReload: Boolean; const callback: ICefJsDialogCallback;
  out Result: Boolean);
begin
  Result := True;
  callback.cont(true, '');
end;
Tested on this page :
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onbeforeunload
Thank you, this did the trick ! :D