Skip Ribbon Commands
Skip to main content
Navigate Up
Sign In

Quick Launch

Ralph Arvesen (vertigo blog) > Posts > Call Silverlight from JavaScript, call JavaScript from Silverlight
May 15
Call Silverlight from JavaScript, call JavaScript from Silverlight
The following demonstrates how to call JavaScript from Silverlight managed code, and Silverlight managed code from JavaScript. Note this is using a beta version of Silverlight which can (and probably will) change in the future.

The main points are...

In the JavaScript code:
  • Use document.getElementById to get the Silverlight control.
  • Then call Content.<registered name>.<method name>. For example, control.Content.Page.UpdateText(text).
  • Nothing special for the functions that are called from Silverlight.
In the Silverlight code:
  • Mark the class with the ScriptableType attribute.
  • Call HtmlPage.RegisterScriptableObject in the constructor.
  • Mark any methods that will be called from JavaScript with ScriptableMember.
  • Use HtmlPage.Window.Invoke to call JavaScript functions.
Here are more detailed steps to call JavaScript and Silverlight code.

Call Silverlight method from JavaScript

1) Add XAML elements to the Silverlight control. This displays the string passed in from JavaScript.
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
  <TextBox x:Name="Result" HorizontalAlignment="Stretch" Width="200" Height="24" />
</StackPanel>
2) Make the object accessible to JavaScript by specifying the ScriptableType attribute and calling RegisterScriptableObject.
[ScriptableType]
public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();

        // make this object a scriptable object
        HtmlPage.RegisterScriptableObject("Page"this);            
    }
3) Add the method that is called from JavaScript and specify the ScriptableMember attribute.
// called from javascript
[ScriptableMember]
public void UpdateText(string result)
{
    this.Result.Text = result;
}
4) Add HTML elements to the web page.
<!-- html area, click on the link to send data to the Silverlight control -->
<input id="result" type="text" size="30" />&nbsp;&nbsp;
<a href="javascript:updateSilverlight();">Update Silverlight</a>
5) Update the ID for the Silverlight control. This is not necessary, but you might want to use something besides the default XAML1.
<asp:Silverlight ID="silverlightControl" ... />
6) Add the JavaScript function that calls the Silverlight managed code method.
function updateSilverlight()
{
    // get data from html control
    var text = document.getElementById("result").value;

    // call silverlight control method
    var control = document.getElementById("silverlightControl");
    control.Content.Page.UpdateText(text);
}
Now you can run the application. Enter some text in the HTML textbox and press the Update Silverlight link. The JavaScript function updateSilverlight is executed which calls the managed code UpdateText method.

image


Call JavaScript function from Silverlight

1) Add the JavaScript function that will be called.
// called from silverlight
function updateText(text)
{
    document.getElementById("result").value = text;

2) Add a button element to the XAML.
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
  <TextBox x:Name="Result" HorizontalAlignment="Stretch" Width="200" Height="24" />
  <Button x:Name="CallJavaScript" Content="Update JavaScript"
     Width
="130" Height="24" Margin="10,0,0,0" Click="CallJavaScript_Click" />

</StackPanel>
3) Implement the click handler in the code-behind file.
private void CallJavaScript_Click(object sender, RoutedEventArgs e)
{
    // call the javascript function
    HtmlPage.Window.Invoke("updateText"this.Result.Text);
}
Now run the application again. Enter some text in the Silverlight textbox and click the Update JavaScript button. The click handler is executed which uses the HtmlPage.Window.Inovke method to execute the JavaScript updateText function.

image

Comments

Great post ... really usefull and easy to follow. Thanks for posting.
System Account on 8/7/2008 12:01 AM

A very goood Post. Thnak You.
System Account on 8/23/2008 7:07 AM

NIrav

Hey Man this is not workin I followed your inst.
for callin jscript from SL but it gives an error
"Failed to Invoke:"updateText" ".Please comment on it
System Account on 9/2/2008 4:01 AM

Thanks for the Article.  One small change for Silverlight2 using C#

Use the fully qualified:
System.Windows.Browser.HtmlPage.Window.Invoke("", "")
System Account on 11/19/2008 6:24 AM

vikas

acceept thanks - verbal
System Account on 2/6/2009 2:20 AM

Jacek Ciereszko

Hi,
Very good article. No extra words, only clear examples!

I also did some time ago something similar but with html <-> Silverlight (link in URL).

Regards,
Jacek
System Account on 7/13/2009 3:25 PM

Imran

Hi,

Read your article about Silverlight and javascript.

Our team are working with Silverlight they have integrated silverlight application into web the ASP.net.

We will be happy to receive your input on this:

1) Once user LogIn into our web and later on go into a silverlight form, we need the same user must be logged in this silver light application.

2) I read several forums and articles people talk about sessionId
  e.g HtmlPage.Document.Cookies("ASP.NET_SessionId") to paas the same session userId for the silverlight page.

However, other solution I thought ASP.net browser generates cookies, so silverlight can capture this cookies to be logged in as same user at silverlight form. but issues is cookies must be encrypted for the security wise.

If you have any sample code which I can integrate into our silverlight which can capture session or cookies from web browser must be great.

Or any other solution if you proposes with some sample code I will be delighted.

Many thanks and looking forward.

Regards
I.
System Account on 10/7/2009 8:42 AM

Imran

Hi very good article.#
"Call Silverlight method from JavaScript" doesnt work becuase your example calls silver light control using old version.

Using version 3.0, I am unable to do so.
thanks!

System Account on 10/13/2009 1:34 AM

Sreekanth MG

Hi,
I am stukup with the below requiremtn. Please help me to come out of this.
I have to invoke a Button click event which is available inside Silverlight xaml page from javascript.
I have tried with the following way:
var slObject = document.getElementById("SLObject");
var box = slObject.content.FindName("btn");
box.Click();
here, btn is my button control id which is placed in xaml page.

The code is not working.

Requesting help to solve this scenario.
System Account on 7/13/2010 3:34 AM

Sreekanth MG

Hi,
I am stukup with the below requiremtn. Please help me to come out of this.
I have to invoke a Button click event which is available inside Silverlight xaml page from javascript.
I have tried with the following way:
var slObject = document.getElementById("SLObject");
var box = slObject.content.FindName("btn");
box.Click();
here, btn is my button control id which is placed in xaml page.

The code is not working.

Requesting help to solve this scenario.
System Account on 7/13/2010 3:41 AM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


CommentUrl


Attachments