LostNomad311
LostNomad311

Reputation: 2065

Manually Writing the HTML in TWebBrowser Pt. 2

As the name suggests this is a continuation (sort of) of Manually Writing the HTML in TWebBrowser

This time around I'm trying to add some auto-refresh logic to the HTML I get. I have pieced together an approach from several sources (see below). In short, I am trying to locate the title node and add a meta node after it (in the HTML head node). But, I get an access violation.

Here is the source:

iHtmlDoc := IHTMLDocument3(WebBrowser1.Document);
iHtmlEleTitle := IHTMLElement2(iHtmlDoc.getElementsByName('title').item(0, 0));
iHtmlEle := IHTMLElement2(IHTMLDocument2(iHtmlDoc).createElement(Format('<meta http-equiv="refresh" content="%d">', [1])));
iHtmlEleTitle.insertAdjacentElement('afterEnd', IHTMLElement(iHtmlEle));

And A (technically not functionally) different way of doing it ...casting is slightly different here:

IHTMLElement2(IHtmlDocument3(WebBrowser1.Document).getElementsByName('title').item(0, 0)).insertAdjacentElement('afterEnd', IHTMLDocument2(WebBrowser1.Document).createElement(Format('<meta http-equiv="refresh" content="%d">', [VPI_ISSUANCE_AUTO_RELOAD])));

Again all I get from Delphi is a access exception, and I fished through MSDN documentation on it, but now I'm hoping someone out there has gone through the same and has some insight. Any help?

Sources (I think this is all of them): http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm (auto-reload)
http://delphi.about.com/od/adptips2005/qt/webbrowserhtml.htm (web browser document as an HTML document)
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.insertadjacentelement(VS.80).aspx (GetElementsByName)
http://www.experts-exchange.com/Web_Development/Components/ActiveX/Q_26131034.html (insertAdjacentElement)
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23407977.html (GetElementsByName)

Upvotes: 0

Views: 1313

Answers (1)

Francesca
Francesca

Reputation: 21650

I don't know where you get your AV, but I would anyway cut the compound expressions into single pieces so you can check that you actually get an interface/element (not nil).

For instance:

  iHtmlEleTitle := IHTMLElement2(iHtmlDoc.getElementsByName('title').item(0, 0));

should be broken like

  iHtmlCol := iHtmlDoc.getElementsByName('title');
  if Assigned(iHtmlCol) then
    iHtmlEleTitle := IHTMLElement2(iHtmlCol.item(0, 0));

You check that iHtmlCol is not nil, which it will if the "title" element is not found

Upvotes: 2

Related Questions