I'm trying to resolve an issue that's disconcerting me. It might be something obvious but I can't really figure out what's wrong.
I have a winform with a WebBrowser which I load this way and it works fine:
WebBrowser.DocumentText = someHtmlCode;
In my winform's webbrowser I can right-click to get the HTML source and check out that the code is the same I set in the DocumentText property... everything's ok at this point.
So once I get it loaded and working, I inject some JavaScript code into it, something like this:
HtmlElement head = WebBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = WebBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
So I'm adding a new JavaScript function named "sayHello" that I can call this way, and it's still working properly:
WebBrowser.Document.InvokeScript("sayHello");
So everything's ok at this point, I can run the JavaScript function and getting the alert inside the WebBrowser, but what's disconcerting me is that when I right-click on the WebBrowser to get the source code, I get the same code I set at the beginning. No trace about the JavaScript code added later, although it's there because it's working when I invoke it.
Of course, if I look into the WebBrowser.HtmlDocument, I can't see the JavaScript either, it's the same that I get looking at the source code.
So my question is: where's the new JavaScript code going and how can I get it back? I need to get the updated HTML code.
Thanks!
Source Code does not give you updated document. To get updated document you have to use following
HtmlElement head = WebBrowser.Document.GetElementsByTagName("head")[0];
string h = head.innerHtml;
The only option is to navigate top node and get innerHtml for current state of Html.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.