troubleshooting Question

Javascript refresh in webbrowser control unintentionally fires FormActivate

Avatar of moonrisesystems
moonrisesystems asked on
Delphi
3 Comments1 Solution889 ViewsLast Modified:
Here's a strange one that has me stumped.

http://www.kioskremote.net/secure/files/display.asp?ZoneID=42

This is an ASP script for an advertising system that displays a set of defined "advertisements" in rotating fashion. The refreshes when the ad panel changes are done via a javascript refresh (load the link above into IE to see a demonstration).

Here's the problem. In a web browser control application, whenever I click on one of these ASP links and loaded the result into a new browser form(Unit2), Unit1's FormActivate event appears to fire everytime the javascript refresh command is called, forcing form2 to the background.

What is even stranger is this only happens when the ASP link is clicked. Normal links opened in form2 are unaffected.

Can anyone explain this weird behaviour?

In the sample application enclosed, the left browser on form1 is the ASP script that gives the trouble and the right browser on form1 is a normal plain HTML page. Form2 represents a pop up browser that opens as a result of clicking on one of the ad panels.

Here's the sample application with two units (unit1(main application), unit2(pop up browser)) to illustrate the problem:

------ unit1 -----
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, WebBrowser,Unit2, StdCtrls;

type
  TForm1 = class(TForm)
    NetStopBrowser: TWebBrowser;
    NetStopBrowser3: TWebBrowser;
    Memo: TMemo;
    procedure FormShow(Sender: TObject);
    procedure NetStopBrowserBeforeNavigate2(Sender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  initial : Boolean;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
  initial := True;
  NetstopBrowser.Navigate('http://www.kioskremote.net/secure/files/display.asp?ZoneID=42');
  initial := True;
  NetstopBrowser3.Navigate('http://www.google.com');
end;

procedure TForm1.NetStopBrowserBeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  if initial then
    initial := False
  else begin
    Cancel := True;
    with TForm2.Create(nil) do begin
      Show;
      NetstopBrowser2.Navigate(URL);
    end;
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  memo.Lines.Add('form activated');
end;

end.
------------------

------ unit2 -----

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, WebBrowser;

type
  TForm2 = class(TForm)
    NetStopBrowser2: TWebBrowser;
    procedure NetStopBrowser2BeforeNavigate2(Sender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.NetStopBrowser2BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  showmessage(URL);
end;

end.
------------------
ASKER CERTIFIED SOLUTION
moonrisesystems

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros