I have desktop application with WebBrowser control and try to inject JavaScript into loaded page.
For this I added two script elements:
private static void AddJQueryElement(HtmlElement head)
{
HtmlElement scriptEl = head.Document.CreateElement("script");
IHTMLScriptElement jQueryElement = (IHTMLScriptElement)scriptEl.DomElement;
jQueryElement.src = @"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
head.AppendChild(scriptEl);
}
private static void AddScriptElement(HtmlElement head)
{
HtmlElement scriptEl = head.Document.CreateElement("script");
IHTMLScriptElement myScriptElement = (IHTMLScriptElement)scriptEl.DomElement;
myScriptElement.src = @"file:///c:\JScript.js";
head.AppendChild(scriptEl);
}
how you can see first is reference to jQuery because I use it in my script. When I try to invoke function from my script using _webBrowser.Document.InvokeScript
WebBrowser throws
Script Error :"Object expected". and points to the line where i try to use jQuery (var tags = $("Body").find("*");
).
How can I prevent this error?
Another interesting thing: if I add something like alert("hello");
to start of my function all works fine.