{"id":2594,"date":"2022-08-30T15:26:10","date_gmt":"2022-08-30T15:26:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/inject-javascript-with-jquery-at-runtime-into-webbrowser-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:10","modified_gmt":"2022-08-30T15:26:10","slug":"inject-javascript-with-jquery-at-runtime-into-webbrowser-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/inject-javascript-with-jquery-at-runtime-into-webbrowser-collection-of-common-programming-errors\/","title":{"rendered":"Inject JavaScript with jQuery at runtime into WebBrowser-Collection of common programming errors"},"content":{"rendered":"<p>I have desktop application with WebBrowser control and try to inject JavaScript into loaded page.<\/p>\n<p>For this I added two script elements:<\/p>\n<pre><code>private static void AddJQueryElement(HtmlElement head)\n{\n    HtmlElement scriptEl = head.Document.CreateElement(\"script\");\n    IHTMLScriptElement jQueryElement = (IHTMLScriptElement)scriptEl.DomElement;\n    jQueryElement.src = @\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.3.2\/jquery.min.js\";\n\n    head.AppendChild(scriptEl);           \n}\n\nprivate static void AddScriptElement(HtmlElement head)\n{\n    HtmlElement scriptEl = head.Document.CreateElement(\"script\");\n    IHTMLScriptElement myScriptElement = (IHTMLScriptElement)scriptEl.DomElement;\n    myScriptElement.src = @\"file:\/\/\/c:\\JScript.js\";\n    head.AppendChild(scriptEl);\n}\n<\/code><\/pre>\n<p>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 <code>_webBrowser.Document.InvokeScript<\/code> WebBrowser throws Script Error :&#8221;Object expected&#8221;. and points to the line where i try to use jQuery (<code>var tags = $(\"Body\").find(\"*\");<\/code>).<\/p>\n<p>How can I prevent this error?<\/p>\n<p>Another interesting thing: if I add something like <code>alert(\"hello\");<\/code> to start of my function all works fine.<\/p>\n<ol>\n<li>\n<p>Haven&#8217;t got correct answer, but have solved the problem by using local copy of jquery.min.js.<\/p>\n<\/li>\n<li>\n<p>It&#8217;s possible you aren&#8217;t specifying your script to run on load. alert(&#8220;hello&#8221;) is buying that time needed to download the script\/finish building the HTML.<\/p>\n<pre><code>$(document).ready(function() {\n  \/\/ Handler for .ready() called.\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 02:37:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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(&#8220;script&#8221;); IHTMLScriptElement jQueryElement = (IHTMLScriptElement)scriptEl.DomElement; jQueryElement.src = @&#8221;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.3.2\/jquery.min.js&#8221;; head.AppendChild(scriptEl); } private static void AddScriptElement(HtmlElement head) { HtmlElement scriptEl = head.Document.CreateElement(&#8220;script&#8221;); IHTMLScriptElement myScriptElement = (IHTMLScriptElement)scriptEl.DomElement; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2594","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2594"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2594\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}