WebBrowser control and InvokeScript

Published 23 September 05 10:24 AM | madsn

This is a little code nugget I planned to blog about a while ago, but forgot, so my recap and reasoning is not a 100% but hopefully it might be of help to someone struggelig with making the System.Windows.Forms.WebBrowser control execute scripts programatically.

I started out using the WebBrowser.InvokeScript method to force execution of clientside scripts in the page that was loaded in my WebBrowser control:

HtmlWindow win = doc.Window;
doc.InvokeScript(
"Update_UI_From_Values();");

This is where my reasoning is not complete (because i didn't bother to actually reproduce the original problem..), but basically it didn't work as expected. After some Reflector action I figured out that the mshtml.IHTMLWindow2.execScript method wasn't actually directly mapped into the new WebBrowser APIs and I figured I'd give it a go and add a reference to Microsoft.mshtml.dll (COM component Microsoft HTML Object Library):

mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)propertyEditorWebBrowser.Document.Window.DomWindow;
win.execScript(
"Update_UI_From_Values();", "javascript");

This had the desired effect and the script executed. Too bad the managed option didn't do the job.

Filed under:

Comments

# andres said on September 23, 2005 08:08 AM:

Testing

# Phillip Pearson said on August 12, 2007 05:45 AM:

At least in C# 2005 Express, the following works:

doc.InvokeScript("Update_UI_From_Values");

The parameter is just a function name... perhaps the "();" after it was what was breaking it for you.

# John said on April 1, 2008 05:51 PM:

I have been running into problems using InvokeScript to return the array from a JavaScript function. Works fine for a single string though.

Any help?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Funstuff

Goodies

MSCRM Blogs

My (old) work

Sharepoint

Useful reading

Weblogs

Syndication