tags:

views:

68

answers:

1

As the name suggests this is a continuation (sort of) of http://stackoverflow.com/questions/2784679/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)

+2  A: 

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

François
I may dig into this stuff deeper, but it took me an hour or two to pull this nonsense together ...honestly I hope I'm doing something very wrong (with all this casting between different versions of the same interfaces). So the point of the post is to save me hours of debugging a functionality I can't really justify spending much more time on. As a side note the other team (that generates the web pages) seems to be willing to add the auto-refresh functionality so I wouldn't even need to figure this out. +1 though, I did intend to clean up the code once I got it working.
nomad311
The point is that navigating an HTML document like that is highly depending on the structure of the document. You cannot go to the next step unless you're sure to have something to start with...
François
I get what you're saying, I analyzed the HTML, wrote the code, got an access violation, put a break point in, and checked that the HTML was as I expected (in this scenario had a title node). The code changes you are proposing, automate that check. Rather the check returns true tells me nothing, except that I don't have what I want and am attempting to retrieve. So we are full circle again. I could spend hours on this, or request help from a more experienced person. I opted to go with the later - and so far it seems as if no one can or will help me on this...
nomad311
Without having your HTML, analyzing its structure and knowing what you expect to get and where, it's extremely hard to pinpoint the problem. You might be looking for the Title Element in the wrong place. It can be a pain. To the point where I've seen some give up with the HtmlDoc, and just parse the stream with Pos, PosEx and such.
François