简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM