views:

41

answers:

1

I'm just trying to call a Javascript function from Java using JSObject. I need the Javascript function to update an HTML text field. For this I need to know the element ID and the value.

I have tried this but it doesn't work. When trying to access from Javascript the values passed are empty.

So my question is how can I access those values from Javascript? I must be missing something.

If I declare objects of size 1 it will work, but I need to pass 2 values in objects array.

Thanks in advance.

//Java code
Object[] objects = new Object[2];
objects[0] = "elementId";
objects[1] = "elementValue";
window.call("updateElement",objects);

//Javascript code
function updateElement(array){
  alert(array[0]);
  alert(array[1]);
}
A: 

So I noticed that you are using an Object array in java and passing in string values. Have you tried using a String array?

String[] x = new String[2];
x[0] = "elementId";
x[1] = "elementValue";
window.call("updateElement", x);

give that a try and see if that works. my guess is that javascript isnt able to realize that object array being passed is a string array. though i am not 100% sure.

Nilesh Tailor
If I do this in my Javascript it works: var args = Array.prototype.slice.call(arguments); I guess I need to parse the arguments in a special way. And the function doesn't take any arguments!
Marquinio