-1

I use a webbrowser control to call a javascript function in a html file using InvokeScript(). I would like to pass four lists as parameters so I can use the data in the javascript function.

pseudocode:

        List<string> list1 = new List<string>();
        list1.Add("foo");
        list1.Add("bar");

        List<string> list2 = new List<string>();
        list2.Add("foo");
        list2.Add("bar");

        List<string> list3 = new List<string>();
        list3.Add("foo");
        list3.Add("bar");

        List<string> list4 = new List<string>();
        list4.Add("foo");
        list4.Add("bar");

        maps_webbrowser.Document.InvokeScript("initialize", list1.ToArray(), list2.ToArray() ,list3.ToArray() ,list4.ToArray());

I have read a post where a list is passed using a argument variable

How should an array be passed to a Javascript function from C#?

Here's an example JavaScript function:

function foo()
{
var stringArgs = [];
for (var i = 0; i < arguments.length; i++)
    stringArgs.push(arguments[i]);

// do stuff with stringArgs
}

And you'd call it from C# like this:

List<string> arguments = new List<string>();
arguments.Add("foo");
arguments.Add("bar");
webBrowser.InvokeScript("foo", arguments.ToArray());

However, in this way only one list is passed.

The pseudocode I've wrote down does not work....

3
  • And your question? What didn't work? How have you declared your js function initialize ? Commented Sep 20, 2014 at 23:43
  • Sorry L. for the unclear post. I was working on the problem all night. After a good night of sleep I got some new ideas :) Commented Sep 21, 2014 at 21:05
  • @L.B I did some research and managed to awnser my question. Any tips? Thanks in advance. Commented Sep 22, 2014 at 22:33

1 Answer 1

1

After a good night of sleep I've basically got it working :)

According to the msdn an object array has to be passed as parameter:

http://msdn.microsoft.com/en-us/library/cc452443(v=vs.110).aspx

C# code:

List<string> lat_waypoints = new List<string>();
lat_waypoints.Add("1.11111");
lat_waypoints.Add("2.12112");

List<string> lon_waypoints = new List<string>();
lon_waypoints.Add("34.1234");
lon_waypoints.Add("34.2345");

string lat_string = string.Join(",", lat_waypoints.ToArray());
string lon_string = string.Join(",", lon_waypoints.ToArray());

Object[] objArray = new Object[2];
objArray[0] = (Object)lat_string;
objArray[1] = (Object)lon_string;

maps_webbrowser.Document.InvokeScript("test", objArray);

Javascript:

<HTML>
<SCRIPT>
    function test(lat, lon) {

    var lat_split = lat.split(",");
    var lon_split = lon.split(",");

        alert("Lat: " +lat_split[0] + " lon: " + lon_split[0]);
    }
</SCRIPT>

<BODY>
</BODY>
</HTML>

This solution works but it is not the nicest solution in my opinion......

The lat and lon values are originally stored in an list with doubles. However, I don't know how to pass an array of doubles directly without converting it to string first.

Anyone else with some ideas?

Sign up to request clarification or add additional context in comments.

1 Comment

@L.B What do you think of this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.