2

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.

2 Answers 2

1

Haven't got correct answer, but have solved the problem by using local copy of jquery.min.js.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Orsol, can you tell me how you connected to the webpage. I mean how your application connects to the page. I really want to do that but can't figure it out how to do that.
I load page in a WebBrowser control.
can you please include your full code ? i am getting $ undefined error. but plain javascript working.
0

It's possible you aren't specifying your script to run on load. alert("hello") is buying that time needed to download the script/finish building the HTML.

$(document).ready(function() {
  // Handler for .ready() called.
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.