• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

Javascript refresh in webbrowser control unintentionally fires FormActivate

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.
------------------
0
moonrisesystems
Asked:
moonrisesystems
1 Solution
 
Eddie ShipmanAll-around developerCommented:
Did you try setting the pDisp in the NetStopBrowserBeforeNavigate2 to the NetStopBrowser2 in Form2
instead of navigating to it there? I think that is the way to "redirect" a navigation to another browser.
0
 
moonrisesystemsAuthor Commented:
Problem is second form not shown modally.  When TWebBrowser refreshes itself it doesn't distinguish between a use initiated navigation or a script initiated navigation.  Either way it grabs the input focus when it has finished pushing the non modal for to the back and it's own form to the front to show it's contents to the user.

Solution: Set the TWebBrowser.Enabled Property to False before displaying second form non modally. This way even though it navigates to a new page because of the Script it does not attempt to acquire input focus.
0
 
LunchyCommented:
Closed, 500 points refunded.
Lunchy
Friendly Neighbourhood Community Support Moderator
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now