Click here to Skip to main content
11,433,997 members (55,663 online)
Click here to Skip to main content

Tagged as

Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript

, 23 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The WebBrowser.Document.InvokeScript() method claims to provide a way to interact with JavaScript code inside of a WebBrowser HTML document. Trying to mess around with a third-party JavaScript over which I had no control (meaning: included in an external webpage that I could not alter), however,...
The WebBrowser.Document.InvokeScript() method claims to provide a way to interact with JavaScript code inside of a WebBrowser HTML document. Trying to mess around with a third-party JavaScript over which I had no control (meaning: included in an external webpage that I could not alter), however, I ran into some problems...

(Code examples will be in C#)

You have to pass on the arguments of the JavaScript function as an object[] argument of the InvokeScript() method. Let's presume you have the following JavaScript function:

function callMe(arg1, arg2) {
	return "arg1 is "+arg1+" and arg2 is "+arg2+"!";
}

You would call it by using:

object[] args = {"argString1", "argString2"};
webBrowser1.Document.InvokeScript("callMe",args);

(Note that you use the function's name as a string without the parenthesis as the first argument of InvokeScript)

This works for functions built-in to JS as well, so for an ordinary alert()-message, you could do the following:

object[] args = {"my important message"};
webBrowser1.Document.InvokeScript("alert",args);

However, I ran into problems when trying to call JS-methods of objects or trying to get the value of a variable. For the latter, if you were able to edit the JS code, you could of course use the following:

function getMyVariableValue() {
	return myVariable;
}

and call it by:

webBrowser1.Document.InvokeScript("getMyVariableValue",args);

retreiving the variable's value.

But as I said, in my case I could not alter the code. For the objects-method issue, maybe it was just me - but something like ...InvokeScript("myObject.doSomething",args) didn't work. So I came up with the following solution:
JavaScript has, for those of you not too familiar with it, a function eval(string) which parses the given string as actual JavaScript code. Normally a possible security threat - bad thing! - it gets quite handy for our purpose.

Knowing this, you can now do the following:

object[] codeString = {"alert('Hello world!');"};
webBrowser1.Document.InvokeScript("eval",codeString);

Executing a method is now as easy...

object[] codeString = {"myObject.setVariable(0);"};
webBrowser1.Document.InvokeScript("eval",codeString);

...as getting the value of a variable:

object[] codeString = {"aVariable;"};
string result = webBrowser1.Document.InvokeScript("eval",codeString);

Note here that InvokeScript really returns the JS data, so would now have the value of the aVariable Javascript variable in the C# variable result. Please make sure not to try to use "return aVariable;" in this case, as this will not return anything because of the data already being returned.

Wrapping it up into a function, you can make its use even more comfortable:

private string sendJS(string JScript) {
	object[] args = {JScript};
	return webBrowser1.Document.InvokeScript("eval",args).ToString();
}

sendJS will be kind of your own personal JS-eval() working within your C# environment.

Hope this is of any use for someone!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

C. Groß

Germany Germany
No Biography provided
Group type: Organisation (No members)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Naren Neelamegam24-Jan-13 3:57
memberNaren Neelamegam24-Jan-13 3:57 
GeneralMy vote of 5 Pin
Ashraf Samy Hegab19-Jan-13 10:06
memberAshraf Samy Hegab19-Jan-13 10:06 
GeneralMy vote of 5 Pin
Ivan Ferrer25-Oct-12 0:31
memberIvan Ferrer25-Oct-12 0:31 
GeneralMy vote of 5 Pin
Jordan Marr2-May-12 6:38
memberJordan Marr2-May-12 6:38 
GeneralReason for my vote of 5 I am new to JS so I didnt know the e... Pin
martin.skuta2-Apr-11 2:31
membermartin.skuta2-Apr-11 2:31 
GeneralReason for my vote of 5 that was a great help :) Pin
Pennpann21-Mar-11 6:18
memberPennpann21-Mar-11 6:18 
QuestionAccess a javascript function's method which returns a DOM <textArea> element with VB.net Pin
Member 811162625-Jul-11 16:06
memberMember 811162625-Jul-11 16:06 
GeneralInvoking WPF from javascript Pin
frankhevans2-Apr-10 12:53
memberfrankhevans2-Apr-10 12:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 24 Feb 2010
Article Copyright 2010 by C. Groß
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid