Running JavaScript function on an instance of Internet Explorer

I use

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer() 

to manage / automate an instance of Internet Explorer. On some pages, I want to run the JavaScript function ( init() ). It seems the best way to do this is to use the HtmlDocument InvokeScript method, and I have not tried the following:

 void ie_DocumentComplete(object pDisp, ref object URL) { System.Windows.Forms.HtmlDocument doc = ie.Document; doc.InvokeScript("init"); } 

Which fails because doc is null. It seems I cannot get System.Windows.Forms.HtmlDocument from ie.Document . Also, having tried the above, I also tried:

 System.Windows.Forms.HtmlDocument doc2 = (System.Windows.Forms.HtmlDocument)ie.Document; 

and

 System.Windows.Forms.HtmlDocument doc2 = ie.Document as System.Windows.Forms.HtmlDocument; 

Any ideas on how I can get this to work, or an even better way to run scripts on the page?

Thanks!!

EDIT

Another way to run a JavaScript function is:

 SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer() mshtml.HTMLDocument doc = ie.Document; mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2; win.execScript("init();", "javascript"); 

But the line

 mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2; 

throws an error that this is an invalid listing ( InvalidCastException ) - although IntelliSense (and MSDN) say that doc.parentWindow is IHTMLWindow2 . Any ideas? (I also made sure that the page was fully loaded before running this code)

+7
c # internet-explorer automation
source share
5 answers

The problem is with streaming processing - I spent so much time on STA problems that you would probably know about now :).

Anyway, I found a way to get the second bit of code that I published while working and running javascript functions in an IE window! Here is the code:

 this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { mshtml.HTMLDocument doc = ie.Document; mshtml.IHTMLWindow2 win = doc.parentWindow as IHTMLWindow2; win.execScript("init();", "javascript"); })); 

Hope this helps someone!

+6
source share

You need to access document.parentWindow in the STA stream. This may help you:

  private WebBrowser _webBrowser; //initialize this somewhere private void ExecuteJavaScript() { Thread aThread = new Thread(ExecuteJavaScriptWorker); aThread.SetApartmentState(ApartmentState.STA); aThread.Start(); } private void ExecuteJavaScriptWorker() { HTMLDocument _document = _webBrowser.Document; _document.parentWindow.execScript("alert('Arbitrary javascript code')", "javascript"); } 
+1
source share

This is an example of how to get a document on a page. This is close to the examples shown above, with a slight (but important) difference - I use the Navigate2 method - this one works correctly.

 public static mshtml.HTMLDocument NavigateTo(String anUrl) { object locEmpty = 0; object locUrl = anUrl; SHDocVw.InternetExplorer _ie = new SHDocVw.InternetExplorer(); _ie.Visible = true; _ie.Navigate2(locUrl, ref locEmpty, ref locEmpty, ref locEmpty, ref locEmpty); return(_ie.Document); } 

This example will work for all pages opened by IE in a normal (Not Modal) window. For modal windows (or modal dialogs) this example does not work.

+1
source share

SHDocVw.InternetExplorer.Document is of type mshtmlHTMLDocumentClass, so you need to refer to Microsoft.mshtml

 mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document; 

The ie object also needs to be moved somewhere so that the document matters. such as

 object test = new object(); ie.Navigate("c:\\tmp\\test1.html", ref test, ref test, ref test, ref test); 

Total init:

 SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer(); object test = new object(); ie.Navigate("c:\\tmp\\test1.html", ref test, ref test, ref test, ref test); mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document; 
0
source share

you can simply:

 ie.Navigate("javascript:" + jsScript); 

where ie is your internetexplorer instance

0
source share

All Articles