views:

408

answers:

2

Hi,

I am using a TWebBrowser component which I use to load XML documents into which are linked to a XSL file.

I have a default page I display when no XML document is loaded. However, if the user deletes the XML file whilst it is open in the browser and then refreshes I get the standard resource could not be found error. What I would like to do instead is if the page cannot be loaded, check that the file exists, and if it doesn't just load the default page again.

I have tried using the OnNavigateError and OnBeforeNavigate2 events however they does not seem to trigger on a refresh.

Any idea's?

+1  A: 

There is an onRefresh event which is exposed by the TWebBrowser replacement TEmbeddedWB. This version also exposes many other features which are otherwise hidden by the TWebBrowser component.

skamradt
A: 

This is a bit of a cludge but it works in my tests, using the standard TWebBrowser component.

What I did was override the F5 key in the form's OnKeyUp event. By setting the form's KeyPreview property to True, you can call your own refresh. Seeing as the TWebBrowser.Refresh method doesn't appear to call any of the navigation events (as you said in your question), I call the TWebBrowser.Navigate event myself, which does fire the events.

You need to store the URL, which I imagine you're already doing. But if not, then in the BeforeNavigate2 event, you are given the URL as a parameter. So store this in a variable or on-screen control. Then, when F5 is pressed (or the Refresh button if you have put one on screen), simpy navigate to that URL again. The OnBeforeNavigate2, OnNavigateComplete2 and OnDocumentComplete events are all fired again, allowing you the opportunity to perform your test and put your placeholder page up instead of IEs default error page.

procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
  Edit1.Text := URL;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_F5) then
  begin
    WebBrowser1.Navigate(Edit1.Text);
  end;
end;
_J_
Isn't the contextmenu of TWebBrowser by default enabled? That would mean a user can also refresh via the contextmenu, which bypasses your check on F5. Samradts suggestion of a replacement control that does surface the OnRefresh event seems better in that case.
Otherside