Ilya Sandler
Ilya Sandler

Reputation: 21

How can I pass array from wpf control to javascript function in webbrowser control?

I can send one argument, but can't send an array.

C# code:

string[] mes = {"Bascketball Stadium", "New Address", "Ilya"};
this.MyWebBrowser.InvokeScript("DaveWriter", mes);

JavaScript code:

function DaveWriter(mes){
   var arr = jQuery.makeArray(mes);         
   document.getElementById('company').innerHTML = arr[0];
   document.getElementById('address').innerHTML = arr[1];           
}

I recieve only the 1st element of the array.

What should I change to recieve all elements?

Upvotes: 0

Views: 613

Answers (1)

dmeglio
dmeglio

Reputation: 2830

Very tricky because the InvokeScript takes the arguments as an array. So for example, you're really passing 3 parameters there, not an array of parameters, each of the elements of mes would be one argument to DaveWriter. Meaning, if you changed it to:

function DaveWriter(arg1, arg2, arg3) {
// Your code
}

It would be there. There isn't, from what I've seen, a clean method to do what you want. Some recommend JSON, then evaluating the JSON in JavaScript. I don't like that idea because you have to use eval() which makes me feel wrong.

Here's what I suggest. What if you pass it like you are, then access it via the arguments variable. See Arguments object for more information. Basically:

function DaveWriter(mes){ 
   document.getElementById('company').innerHTML = arguments[0];
   document.getElementById('address').innerHTML = arguments[1];           
}

It's not ideal, but it will allow you to do what you want. I don't believe there are many other options. Maybe someone knows of a cleaner way, but this should work for you. I didn't have an opportunity to test that fully as I don't have a WPF application with a WebBrowser handy, but I think that will accomplish what you need.

Upvotes: 1

Related Questions