0

I am using a webBrowser control as the main output window for my application. The first time I use document.write(), the text is completely replaced. The second time the text that I am writing is appended to the end. I have confirmed that I am not writing twice or adding strings too many times by accident. Why would the control behave this way?

Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
  • 1
    One typical mistake is to not wait for the DocumentCompleted event before changing the DOM. – Hans Passant Dec 21 '11 at 16:01
  • Shouldn't the errors be present every time then, and not just the first? And most of the time I am using: while (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); to wait. – Nathan Tornquist Dec 21 '11 at 16:15
  • You haven't mentioned errors before, but using DoEvents() is indeed an excellent way to shoot the foot. http://stackoverflow.com/questions/5181777/use-of-application-doevents/5183623#5183623 – Hans Passant Dec 21 '11 at 16:18
  • Thanks for that link. I am only using DoEvents() to let the webBrowser text finish loading. It does not seem to have caused any issues, and prevented the program from crashing when hundreds of messages were loaded from history. – Nathan Tornquist Dec 21 '11 at 16:29

2 Answers2

1

Call OpenNew before writing the document.

HtmlDocument doc = webBrowser1.Document.OpenNew(true);
doc.Write("<HTML><BODY>This is a new HTML document.</BODY></HTML>");
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I am using the webBrowser as output from a chat system. It is better if the Write command only appends text. Regardless of if there is text in the document or not though, the first time it is run everything is overwritten. Do you have any idea why this is? I can just write to it twice on initialization if I need to, but that seems like a workaround. I would like to actually solve the problem if possible. – Nathan Tornquist Dec 21 '11 at 16:11
  • Instead of using `Write`, what if you use `InnerHTML` (http://msdn.microsoft.com/en-us/library/ms533897%28v=vs.85%29.aspx). You can constantly append to the DOM element. – keyboardP Dec 21 '11 at 16:17
  • Is there a way to use Write or DocumentText to completely replace the text of the displayed webBrowser without causing a navigation event to occur? Because it is chat output, all navigation events are blocked and handled manually, this allows me to have image popup windows and open firefox through links and such. EDIT: Because the write command replaces text once, my document ends up being
    ...
    ...
    Content
    – Nathan Tornquist Dec 21 '11 at 16:24
  • I'm not too familiar with the methods. An alternate idea could be to implement a Javascript method into the page which appends the HTML. Then, from your codebehind, you can WebBrowser.InvokeScript and call the Javascript method provding the new text as the parameter. Since it's all done in JS, there won't be a refresh and you can append/overwrite however you like. – keyboardP Dec 21 '11 at 16:27
  • I rewrote the core section of my application so that messages can be added and removed with javascript. You can preview it here: http://nathantornquist.com/code/misc/index6.html I still have a problem however. If you add a message from John, the date flashes like it should. Jack's dates do not flash, and should not. When Jack sends a message though, John stops flashing. Those dates should only stop when the received button is clicked. And when it is clicked, all message dates should be black. Not just most of them. Any ideas? – Nathan Tornquist Dec 21 '11 at 17:47
  • I'm running Firefox and John's dates do not seem to be flashing for me. They're green, but not flashing. Jack's are black and they don't flash either. Just tried it in Chrome and it is flashing so the inconsistency might be part of the problem. – keyboardP Dec 21 '11 at 17:52
  • If you add messages from John first, they flash. After that they do not. The timer that regulates colors should continue to run though. I have no idea why it would stop. Every time the timer fires and changes a color it is supposed to run itself again. – Nathan Tornquist Dec 21 '11 at 17:53
  • Strange, I just did a full-cache refresh in FF and it now flashes as well. If you're storing all of the dates in an array, have you tried iterating through that array and changing their class to 'black'? I noticed there's a dateStandard class, but you could iterate through again and change them to black as well. – keyboardP Dec 21 '11 at 17:54
  • It flashed before and after you send a message from Jack? I can't rely on the cache being refreshed regularly though. – Nathan Tornquist Dec 21 '11 at 17:55
  • Nah, I think there was a problem on my side. It's working like you expect (although with the colour bug). (Updated my previous comment) – keyboardP Dec 21 '11 at 17:57
0

If that doesn't work for you here is another alternative,

webBrowser1.DocumentText = "<HTML><BODY>I am Vinod Srivastav</BODY></HTML>";

which sets the HTML contents of the page displayed in the WebBrowser control.

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40