I\'ve tried this:
string newScript = textBox1.Text;
HtmlElement head = browserCtrl.Document.GetElementsByTagName(\"head\")[0];
HtmlElement scriptEl = browser
this is a solution using mshtml
IHTMLDocument2 doc = new HTMLDocumentClass();
doc.write(new object[] { File.ReadAllText(filePath) });
doc.close();
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);
IHTMLScriptElement scriptObject = (IHTMLScriptElement)doc.createElement("script");
scriptObject.type = @"text/javascript";
scriptObject.text = @"function btn1_OnClick(str){
alert('you clicked' + str);
}";
((HTMLHeadElementClass)head).appendChild((IHTMLDOMNode)scriptObject);
What you want to do is use Page.RegisterStartupScript(key, script) :
See here for more details: http://msdn.microsoft.com/en-us/library/aa478975.aspx
What you basically do is build your javascript string, pass it to that method and give it a unique id( in case you try to register it twice on a page.)
EDIT: This is what you call trigger happy. Feel free to down it. :)
I used this :D
HtmlElement script = this.WebNavegador.Document.CreateElement("SCRIPT");
script.SetAttribute("TEXT", "function GetNameFromBrowser() {" +
"return 'My name is David';" +
"}");
this.WebNavegador.Document.Body.AppendChild(script);
Then you can execute and get the result with:
string myNameIs = (string)this.WebNavegador.Document.InvokeScript("GetNameFromBrowser");
I hope to be helpful
For some reason Richard's solution didn't work on my end (insertAdjacentText failed with an exception). This however seems to work:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");
This answer explains how to get the IHTMLScriptElement
interface into your project.
Also, in .NET 4 this is even easier if you use the dynamic keyword:
dynamic document = this.browser.Document;
dynamic head = document.GetElementsByTagName("head")[0];
dynamic scriptEl = document.CreateElement("script");
scriptEl.text = ...;
head.AppendChild(scriptEl);
The managed wrapper for the HTML document doesn't completely implement the functionality you need, so you need to dip into the MSHTML API to accomplish what you want:
1) Add a reference to MSHTML, which will probalby be called "Microsoft HTML Object Library" under COM references.
2) Add 'using mshtml;' to your namespaces.
3) Get a reference to your script element's IHTMLElement:
IHTMLElement iScriptEl = (IHTMLElement)scriptEl.DomElement;
4) Call the insertAdjacentText method, with the first parameter value of "afterBegin". All the possible values are listed here:
iScriptEl.insertAdjacentText("afterBegin", "function sayHello() { alert('hello') }");
5) Now you'll be able to see the code in the scriptEl.InnerText property.
Hth, Richard