Link to home
Start Free TrialLog in
Avatar of ant84
ant84

asked on

MSHTML edit the <head> section of a document

I have an mshtml.IHTMLDocument which I would like to add a new stylesheet to.

The only way to add a stylesheet is in the <head> section, which it seems I cannot edit in anyway.  

Does anyone have any recommendations on how it can be done?
Avatar of vo1d
vo1d
Flag of Germany image

it can be edit.
Avatar of ant84
ant84

ASKER

Thanks i'll give it a go and let you know how i get on.
Avatar of ant84

ASKER

That looks like it'll do the trick, i am running into a couple of problems though with finding a method to edit the inner html of the <style> node.  Is there such a way you can think of?  
Avatar of ant84

ASKER

Here is my code:


            mshtml.HTMLDocument htmlDocument;

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

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

Then:

        private void ProcessNode(mshtml.IHTMLDOMNode node)
        {            
            if (node is mshtml.HTMLHeadElement)
            {
                foreach (mshtml.IHTMLDOMNode cNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
                {
                    //there doesn't seem to be a cNode.innerHTML property
                }


                //it doesnt let me instatiate a new mshtml.IHTMLDOMNode like this!

                mshtml.IHTMLDOMNode newNode = new mshtml.IHTMLDOMNode();
                ((mshtml.HTMLLinkElement)newNode).setAttribute("rel", "stylesheet", 0);
                ((mshtml.HTMLLinkElement)newNode).setAttribute("href", "stylesheets/paviors.css", 0);

                node.appendChild((mshtml.IHTMLDOMNode)newNode);

            }
            if ((node.childNodes as mshtml.IHTMLDOMChildrenCollection).length == 0)
            {
                return;
            }
            foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
            {
                ProcessNode(childNode);
            }
        }

I raised the points on this question, I've been experimenting for much of the day and still can't get it working
maybe this works for you:

private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            mshtml.HTMLDocument htmlDocument = (mshtml.HTMLDocument)this.axWebBrowser1.Document;

            foreach (mshtml.IHTMLStyleSheet styleSheet in (htmlDocument.styleSheets as mshtml.HTMLStyleSheetsCollection))
            {
                 //check the styleSheet ref here
            }            
        }

in this case i think, the ProcessNode method will not fit to your needs.
another problem could be, that the webpage is loaded and remains in memory, from were it will be rendered in the browser.
so it could be, that you will not see the changes of your stylesheet.
therefore i would recommend, to temporary save the modified document and load it again in the browser.
but therefore, you will need a flag to check, in which state you are.
maybesomething like this:
private bool loadTempDocument = false;

if you have modified your document with your stylesheet, save it to your harddisk, set the flag to true, load the saved document again and set the flag back to false.
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
Avatar of ant84

ASKER

Many thanks for your help, vo1d.  That was just the trick and I can now reliably edit the <head> section.