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

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
0
raheelasadkhan
Asked:
raheelasadkhan
  • 6
  • 4
1 Solution
 
raheelasadkhanAuthor Commented:
In simpler words, how do I enumerate all the elements within an HTMLDocument and determine what type of element each is?
0
 
vo1dCommented:
try this to process all anchors:

extend your eventhandler 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;

     foreach(mshtml.IHTMLDOMNode node in (htmlDocument.childNodes as mshtml.IHTMLDOMChildrenCollection))
     {
                ProcessNode(node);            
     }
}

now the ProcessNode method:

private void ProcessNode(mshtml.IHTMLDOMNode node)
{            
  if (node is mshtml.HTMLAnchorElement)
  {
    //make your changes here for an anchor
  }
  if ((node.childNodes as mshtml.IHTMLDOMChildrenCollection).length == 0)
  {
     return;
  }
  foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
  {
    ProcessNode(childNode);
  }
}

hope this will help.
0
 
raheelasadkhanAuthor Commented:
Yes that looks about right. I'll give it a try and get back to you. Thanks.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
raheelasadkhanAuthor Commented:
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.
0
 
raheelasadkhanAuthor Commented:
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.
0
 
vo1dCommented:
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.
0
 
raheelasadkhanAuthor Commented:
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.
0
 
vo1dCommented:
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.
0
 
vo1dCommented:
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.
0
 
raheelasadkhanAuthor Commented:
Yeh, the switch to 2.0 is inevitable. Thanks again.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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