Xaser
Xaser

Reputation: 2146

Change contents without redirecting to about:blank

I'm currently working on an application that modifies a specific web page to hide irrelevant information and the displays it in a WebBrowser control in the application window. Unfortunately as soon as i set the DocumentText Property of the WebBrowser, it navigates to about:blank and the displays the HTML content. However, because it redirects to about:blank, all relative element in the web page become invalid, creating a very odd looking web page with no stylesheet what so ever.

Is there a way i can modify what the WebBrowser control displays, without having it redirect to about:blank and therefore ruining all relative elements?

Upvotes: 1

Views: 189

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

This should work for injecting a HTML Element to your page, without resetting the rest of the DOM:

private void button1_Click(object sender, EventArgs e)
{
    HtmlElement myElem = webBrowser1.Document.CreateElement("input");
    dynamic element = myElem.DomElement;

    element.SetAttribute("value", "Hello, World!");
    (webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(myElem);
}

Upvotes: 0

Related Questions