1

I immediately apologize for any misnomers that I may use.

I have a series of web pages that are generated by PHP that I am manipulating using the WebBrowser control. These pages have defined JavaScript functions buried inside the body (which is stupid to me, I didn't write them, I just have to work with them).

Is it possible to access these functions by using the Document.InvokeScript method, and if not, what can I do to access them?

Will
  • 281
  • 1
  • 6
  • 18

3 Answers3

1

Windows forms's implementation uses IDispatch.GetIDsOfNames to look for an exact match of the function name in the top level named items of the script engine. For MSHTML's JScript implementation of active script interfaces, the top level named items are

You don't really need to add new named items to the script engine (you can do so via IDispatchEx if you really want to), there are many ways to execute code using existing named items, for example

  • the eval function from jscript globals
  • the execScript function from the window object
  • the setTimeout function from the window object

You can use one of above as the name of the script method to invoke in HtmlDocument.InvokeScript and pass the code in additional parameters.

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
0

Yes - all functions in JavaScript are equally accessible.

Notes

  • some JavaScript files may not be loaded very early - you need to wait properly (either for document ready/complete, or custom notification from page/simple timeout) till all scripts are loaded.
  • One can make JavaScript functions "private" - not callable by using window.FuncitionName(...) in JavaScript - you will not be able to call such functions via InvokeScript either.

Sample of such "private" function below:

$(function(){  
   ....
   function MyHiddenFunction() {
     // you can't call be from outside!!!!
   }
});
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • So would this explain why when I try to interact directly with the web page loaded into the webbrowser control I get a javascript error? Because the functions haven't been loaded yet and "don't exist"? Do they load quicker if they are located in the header? – Will Jan 24 '13 at 17:39
  • No idea why your page fails. As for referencing JavaScript files/adding JavaScript in HEAD: JavaScript parsed in order they show up in document, so yes - once in HEAD will be loaded very early. – Alexei Levenkov Jan 24 '13 at 17:48
0

I found the answer (Something happened and I can't access my account anymore so if someone can answer me that too that'd be swell) but the answer is this: WebBrowser uses IE7 and IE7 is a piece of crap.

IE9 executes the script just fine. So I have to force it to use IE9

This was done by using the answer given here:

WebBrowser control and JavaScript errors

So... hooray!

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107