Raha
Raha

Reputation: 1969

Call JavaScript function from Silverlight 4.0 application

I am trying to call a function from a Silverlight application. It should be a very simple task to do but so far I am not getting the result that I am looking for.

This is my Silverlight code:

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        HtmlPage.Window.Invoke("SayHello", new string[] { "Salut!" });
    }

And this is the JavaScript code :

   function SayHello(theid) {
        alert(eval(theid));
        var divStatusDiv = document.getElementById("divStatus");
        divStatusDiv.style.backgroundColor = "Red";
    }

The alert message always show "undefined" but when I press "OK" the colour of that DIV gets changed to Red as it should be.

Why am I getting "Undefined" all the time ?

Upvotes: 1

Views: 3555

Answers (3)

nyxtom
nyxtom

Reputation: 2929

You need to create the json that can be passed properly instead of just passing along an array like that. You can simply return "Salut!" instead of new string[] { "Salut!" } or you can create the json array for the string array you have.

Upvotes: 2

Srikar Doddi
Srikar Doddi

Reputation: 15599

Use

alert(eval(theid.value));

Upvotes: 0

Adam
Adam

Reputation: 1874

I'm not familiar with Silverlight, but if theid has value "Salut!" inside of SayHello, then you cannot eval it, since it is a string of text, not code. You should change the line alert(eval(theid)); to just alert(theid);.

Upvotes: 1

Related Questions