Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How to handle beforeunload ?

Post Reply
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

How to handle beforeunload ?

Post 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.
You do not have the required permissions to view the files attached to this post.
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to handle beforeunload ?

Post 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
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to handle beforeunload ?

Post 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.
  ///
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Re: How to handle beforeunload ?

Post 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
Post Reply