Link to home
Start Free TrialLog in
Avatar of raheelasadkhan
raheelasadkhanFlag for Pakistan

asked on

Using MSHTML to modify HTML Elements

Hello,

My application composes an HTML page at run time using content pulled from the net. Once composed, it is saved to file and displayed in aWeb Browser control.

Once displayed I need to add the (TARGET="_blank") atribute to all anchor tags. I am using MSHTML as follows:

private void wbUserRSS_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
      // Force all links in the document to open external windows

      mshtml.HTMLDocument      htmlDocument;

      htmlDocument      = (mshtml.HTMLDocument) this.wbUserRSS.Document;

      // How to access all the <A> elements in the document?
}

Also, are there any good articles out there explaining possibilities with MSHTML in an easy way (unlike the MSDN documentation)?

Thanks,

Khan
Avatar of raheelasadkhan
raheelasadkhan
Flag of Pakistan image

ASKER

In simpler words, how do I enumerate all the elements within an HTMLDocument and determine what type of element each is?
ASKER CERTIFIED SOLUTION
Avatar of vo1d
vo1d
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes that looks about right. I'll give it a try and get back to you. Thanks.
I added the following line to the processing section:
((mshtml.HTMLAnchorElement) node).target      = "_blank";


This seems to have no effect. The links that were opening in the same window before are stilldoing the same.
My bad. My example can update the attribute value but does nothing if the attribute is not present. This works:
((mshtml.HTMLAnchorElement) node).setAttribute("TARGET", "_blank", 0);

Thanks again.

On another note, is there a good documentation source or article around with "How to's"? The MSDN documentation is very unfriendly for MSHTML.
you should take a look after the page is loaded in the source of the page, i dont think that you had changed the correct tags, because the page is finished with loading and remains in memory.
On your last comment, if you alter HTML at runtime via the DOM like we are doing, the changes DO NOT show up in 'View Source'. At least that was my observation.
oh ok, great that it works. i dont know about any documentations for that component. i had done that example only with debugging and looking at the webbrowsers fields.
maybe you could take a look in the msdn for net2, because microsoft integrated now a webbrowser component, which is a managed wrapper for the component, you used. maybe you can find more information there.
Yeh, the switch to 2.0 is inevitable. Thanks again.