Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 993229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:23:20+00:00 2026-05-16T06:23:20+00:00

I have a TWebBrowser component that show a Google maps page. The problem is

  • 0

I have a TWebBrowser component that show a Google maps page. The problem is that when user press F5 the page refresh and page reloads. This cause javascript variables to reinitialize and get out of sync with Delphi and a scripting error dialog appear,
‘undefined’ is null or not an object.

I want to stop refresh from the user.

I tried this event for OnBeforeNavigate2:

procedure TNewOrganizationForm.mapAddressBeforeNavigate2(ASender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  inherited;
  Cancel := Assigned(fMapEngine) and not fMapEngine.Loading;
end;

But when I set a breakpoint it is not even called. Is there another way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:23:21+00:00Added an answer on May 16, 2026 at 6:23 am

    Ronald you can use the IHTMLDocument2.onkeydown event to intercept and block a key.

    to assign an event handler first you must create a procedure type using the IHTMLEventObj as parameter.

      THTMLProcEvent = procedure(Sender: TObject; Event: IHTMLEventObj) of object;
    

    then you must create an class descendent from InterfacedObject and IDispatch to pass and process the events .

    finally you can process the intercepted key in the onkeydown event in this way

    Var
      HTMLDocument2 : IHTMLDocument2;
    begin
        if Not Assigned(WebBrowser1.Document) then  Exit;
        HTMLDocument2:=(WebBrowser1.Document AS IHTMLDocument2);
        if HTMLDocument2.parentWindow.event.keyCode=VK_F5 then //compare the key
        begin
         HTMLDocument2.parentWindow.event.cancelBubble:=True; //cancel the key
         HTMLDocument2.parentWindow.event.keyCode     :=0;
        end;
    end;
    

    //check the full source code

    unit Unit55;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleCtrls, SHDocVw, MSHTML;
    
    type
      //Create the procedure type to assign the event
      THTMLProcEvent = procedure(Sender: TObject; Event: IHTMLEventObj) of object;
    
      //Create a  new class for manage the event from the twebbrowser
      THTMLEventLink = class(TInterfacedObject, IDispatch)
      private
        FOnEvent: THTMLProcEvent;
      private
        constructor Create(Handler: THTMLProcEvent);
        function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
        function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
        function GetIDsOfNames(const IID: TGUID; Names: Pointer;
          NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
        function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
          Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
      public
        property OnEvent: THTMLProcEvent read FOnEvent write FOnEvent;
      end;
    
      TForm55 = class(TForm)
        WebBrowser1: TWebBrowser;
        procedure FormShow(Sender: TObject);
        procedure WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        FOnKeyDownConnector:  THTMLEventLink; //pointer to the event handler
        procedure WebBrowser1OnKeyDown(Sender: TObject; EventObjIfc: IHTMLEventObj);//the event handler 
      public
        { Public declarations }
      end;
    
    var
      Form55: TForm55;
    
    implementation
    
    {$R *.dfm}
    
    
    constructor THTMLEventLink.Create(Handler: THTMLProcEvent);
    begin
      inherited Create;
      _AddRef;
      FOnEvent := Handler;
    end;
    
    
    function THTMLEventLink.GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
    begin
      Result := E_NOTIMPL;
    end;
    
    
    function THTMLEventLink.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult;
    begin
      Result := E_NOTIMPL;
    end;
    
    
    function THTMLEventLink.GetTypeInfoCount(out Count: Integer): HResult;
    begin
      Result := E_NOTIMPL;
    end;
    
    
    function THTMLEventLink.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
    var
      HTMLEventObjIfc: IHTMLEventObj;
    begin
      Result := S_OK;
      if Assigned(FOnEvent) then FOnEvent(Self, HTMLEventObjIfc);
    end;
    
    
    
    procedure TForm55.FormCreate(Sender: TObject);
    begin
      FOnKeyDownConnector := THTMLEventLink.Create(WebBrowser1OnKeyDown); //assign the address of the event handler
    end;
    
    
    procedure TForm55.WebBrowser1NavigateComplete2(ASender: TObject;  const pDisp: IDispatch; var URL: OleVariant);
    var
      HTMLDocument2      : IHTMLDocument2;
    begin
      HTMLDocument2:=(WebBrowser1.Document AS IHTMLDocument2);
      HTMLDocument2.onkeydown := FOnKeyDownConnector as IDispatch; //assign the event handler
    end;
    
    procedure TForm55.WebBrowser1OnKeyDown(Sender: TObject; EventObjIfc: IHTMLEventObj);
    Var
      HTMLDocument2 : IHTMLDocument2;
    begin
        //finally do your stuff here, in this case we will intercept and block the F5 key.
        if Not Assigned(WebBrowser1.Document) then  Exit;
        HTMLDocument2:=(WebBrowser1.Document AS IHTMLDocument2);
        if HTMLDocument2.parentWindow.event.keyCode=VK_F5 then
        begin
         HTMLDocument2.parentWindow.event.cancelBubble:=True;
         HTMLDocument2.parentWindow.event.keyCode     :=0;
        end;
    end;
    
    
    
    procedure TForm55.FormShow(Sender: TObject);
    begin
    WebBrowser1.Navigate('www.google.com'); 
    end;
    
    
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.