Sparers
Sparers

Reputation: 433

How do i execute javascript that was loaded from the DOM

I am trying to execute a function in a javascript that has been loaded via the DOMDocument. For instance:

'on page load
Webbrowser1.navigate("a htmldocument with a div called mainDiv")

Then later:

mDoc = WebBrowser1.Document
Dim mainDiv As IHTMLDOMNode = mDoc.DomDocument.getElementById("mainDiv")
mainDiv.innerHTML = (IO.File.ReadAllText("a file with just a div and script"))
'File has no html, head and body tags

So now i need to execute the script that was loaded retrospectively into mainDiv. i've tried:

Webbrowser1.Document.InvokeScript("onLoadScript")

...but as far as i can gather, this method sees only the DOM loaded from the navigate event. I'm hoping that there is a way of executing a script by accessing the DOMDocument. Any help appreciated.

Thanks

Upvotes: 1

Views: 667

Answers (1)

Louis Ricci
Louis Ricci

Reputation: 21086

You could try injecting a script that calls your dynamic script, dynamically. This bypasses the .InvokeScript function

HtmlElement headtag = WebBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scripttag = WebBrowser1.Document.CreateElement("script");
IHTMLScriptElement scriptelm = (IHTMLScriptElement)scripttag.DomElement;
scriptelm.text = "onLoadScript();";
headtag.AppendChild(scripttag);

Upvotes: 2

Related Questions