Working with JavaScript in Silverlight 4.0

In this post I will show you calling of Javascript function from Silverlight. It is very simple and straight forward.

Assume you have a JavaScript function on aspx page as below,


<script type="text/javascript" language="javascript" >
         function callmeonPageLoad() {
             alert("Hello Javascript from Silvertlight");

You want to call this Javascript function on page load of the Silverlight page then you will have to add namespace


And in the constructor of the page call it as below,


using System.Windows.Controls;
using System.Windows.Browser;

namespace SilverlightApplication7
    public partial class MainPage : UserControl
        public MainPage()
            HtmlPage.RegisterScriptableObject("MainPage", this);


If you notice above code you would find that MainPage is attributed with ScriptableType.


On running Silverlight application you will be getting output as below,


On your XAML if you have textbox like below,


You want to access and change value of textbox txtName in Javascript then it too is very simple. Create a function Javascript as below,


 <script type="text/javascript">
        function UpdatingTextBoxValue(sender)

            var txtBlockFromSL = sender.FindName("txtName");
            txtBlockFromSL.Text = "Salsa ";


txtName is name of the Silverlight text box.

And on the page load on aspx page add a param


If you have a function in managed code and you want to access that from javacript .Assume you have function as below,


       public void SilverLightFunction(string txtToUPdate)
            txtName.Text = txtToUPdate;

You can call this function from JavaScript as below,

 <script type="text/javascript">
        function UpdatingTextBoxValue(sender)

            var host = sender.GetHost();


This was all about various ways to work with Silverlight and JavaScript. I hope this post was useful. Thanks for reading.

4 responses to “Working with JavaScript in Silverlight 4.0”

  1. […] Working with JavaScript in Silverlight 4.0 […]

  2. H Joseph D’silva

    very useful post

  3. H Joseph D’silva

    Very clear

  4. Thanks for the Simple description.
    I was wondering if i could use your technique to Override the CTRL+F functionality in my Silverlight application. Could you suggest how ??

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at