Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #15 – Communicating between JavaScript & Silverlight

Communicating between Javascript and Silverlight is, fortunately, relatively straight forward. The following sample demonstrates how to make the call both ways.

Calling Silverlight From Java script:

  1. In the constructor of your Silverlight app, make a call to RegisterScriptableObject().This call essentially registers a managed object for scriptable access by JavaScript code. The first parameter is any key name you want to give. This key is referenced in your Javascript code when making the call to Silverlight.
  2. Add the function you want called in your Silverlight code. You must prefix it with the [ScriptableMember] attribute.
  3. In Javascript, you can now call directly into your Silverlight function. This can be done through the document object. From my example below: document.getElementById("silverlightControl").Content.Page.UpdateText("Hello from Javascript!"); where “silverlightControl” is the ID of my Silverlight control.

Calling Javascript From Silverlight:

  1. Javascript can be directly called via the HtmlPage.Window.Invoke() function.

Example for both:

Page.xaml:

namespace SilverlightApplication
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
 
            HtmlPage.RegisterScriptableObject("Page", this);            
            HtmlPage.Window.Invoke("TalkToJavaScript", "Hello from Silverlight");
        }
 
        [ScriptableMember]
        public void UpdateText(string result)
        {
            myTextbox.Text = result;
        }
    }
} 

Default.aspx:

<script type="text/javascript"> 
        function TalkToJavaScript( data)
        {
            alert("Message received from Silverlight: "+data);
            
            var control = document.getElementById("silverlightControl");            
            control.Content.Page.UpdateText("Hello from Javascript!");
        }    
</script>

Page.xaml:

<UserControl x:Class="SilverlightApplication7.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="myTextbox">Waiting for call...</TextBlock>
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Communicating between Javascript and Silverlight is, fortunately, relatively straight forward. The following

# July 8, 2008 11:09 AM

Silverlight and JavaScript | DavideZordan.net said:

Pingback from  Silverlight and JavaScript | DavideZordan.net

# July 8, 2008 2:26 PM

Community Blogs said:

Martin Mihaylov on the MultiscaleImage control, Tamir Khason on Visual Tree and thickness, Mike Snow

# July 9, 2008 2:21 AM

Dew Drop - July 9, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 9, 2008 | Alvin Ashcraft's Morning Dew

# July 9, 2008 11:08 AM

Mirrored Blogs said:

Post: Approved at: Jul-9-2008 Using Silverlight to Create a Video Player http://michaelsync.net/2008

# July 9, 2008 4:17 PM

Visual Web Developer Team Blog said:

6 new Silverlight tutorials are completed! Tip of the Day #15 - Communicating between JavaScript &amp;

# July 17, 2008 5:19 PM

SilverlightBlog.com said:

JavaScript

# July 24, 2008 7:06 PM

Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner said:

Pingback from  Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner

# July 28, 2008 7:06 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  &raquo; Blog Archive   &raquo; ??????????????? ??? ??????

# January 14, 2009 8:57 PM

ArneHB said:

document.getElementById is the id you find in the aspx/html file where the Silverlight is called. Took a while before I figured this out.

Example: (In SilverlightAppTestPage.aspx - The statup page)

<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApp.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

Then it should say: var control = document.getElementById("Xaml1");  because Xaml1 is the id to the silverlight as said above.

Other then that I like this tip very much, simple and easy :)

# May 11, 2009 7:48 AM

mike.snow said:

Good point, I always just my ID to make more sense but I should call that out. Thanks.

# May 11, 2009 10:36 AM

jigs_jignesh said:

really helpful and nice artical.....

# November 2, 2009 2:37 AM

miridfd said:

What could be the reason for the following error:

System.InvalidOperationException was unhandled by user code

 Message=Failed to Invoke: clickGlobe.

I tried to invoke a jscript function through silvelight, just as shown above.

It has to be said that the jscript function works fine by invoking it within its page's scope.

Thanks in advance

# April 19, 2010 2:35 PM

mike.snow said:

Not sure, if you post your entire code or send it to me i can better understand why it might be failing. Thanks

# April 19, 2010 2:45 PM

sathi said:

the same error iam getting as explained by miridfd

# June 22, 2010 6:30 AM

Code Interaction in Silverlight « Ramani Sandeep's Blog said:

Pingback from  Code Interaction in Silverlight &laquo; Ramani Sandeep&#039;s Blog

# June 28, 2010 7:12 AM

Silverlight Unsaved Data Detection » Technology Articles - Technology And Programming Articles said:

Pingback from  Silverlight Unsaved Data Detection &raquo; Technology Articles - Technology And Programming Articles

# October 4, 2010 3:47 PM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:06 AM

sandeep.cs3 said:

Good explanation of Communication between Silverlight and JavaScript

www.a2zmenu.com/.../Calling-Silverlight-Method-from-JavaScript.aspx

# October 23, 2010 3:51 AM

Exam Preparation–Silverlight 4, Development–70-506–Part 2 « Mark Monster said:

Pingback from  Exam Preparation&ndash;Silverlight 4, Development&ndash;70-506&ndash;Part 2 &laquo;  Mark Monster

# October 26, 2010 3:48 PM

Using Silerlight and JavaScript to prevent leaving a webpage | jonny's blog said:

Pingback from  Using Silerlight and JavaScript to prevent leaving a webpage | jonny&#039;s blog

# January 23, 2011 11:56 AM

Getting ready for Microsoft Silverlight Exam 70-506 (Part 5) | www.nalli.net said:

Pingback from  Getting ready for Microsoft Silverlight Exam 70-506 (Part 5) | www.nalli.net

# February 14, 2011 8:20 AM

Community Blogs said:

SilverlightShow and Gill Cleeren start a series of materials aimed at helping you get prepared for taking

# February 14, 2011 8:26 AM

Getting ready for Microsoft Silverlight Exam 70-506 (Part 5) | www.nalli.net said:

Pingback from  Getting ready for Microsoft Silverlight Exam 70-506 (Part 5) | www.nalli.net

# February 14, 2011 4:56 PM

razi2000 said:

you helped me very much!

Thanks!!

# February 27, 2011 1:04 PM

Guilli said:

I had the following js literal object:

var obj = {

   func: function(par){

        alert(par);

   }

};

When I called it from Silverlight I got a ScriptObject_InvokeFailed exception:

HtmlPage.Window.Invoke("obj.func", "SomeText");

I solved the problem removing the js function from the literal object:

function func(par){

   alert(par);

}

Doing this, I can now call the js function from Silverlight without any problem:

HtmlPage.Window.Invoke("func", "SomeText");

I assume that maybe HtmlPage.Window.Invoke doesn't like '.' in the function name.

Has anybody experienced the same thing?

Thanks!

# April 29, 2011 10:05 AM

baf110 said:

Very grateful for your code

# July 15, 2011 2:06 AM

Is it possible to interact between ASP.Net page and Silverlight? - Programmers Goodies said:

Pingback from  Is it possible to interact between ASP.Net page and Silverlight? - Programmers Goodies

# August 19, 2011 10:42 AM