Advertisement

Calling Javascript functions from Silverlight 2

When working with Silverlight 2, most will be working with managed code (c#, vb, etc.).  But likely people are working with Silverlight as an additive value to their web application, providing some enhanced user experience to an application.  there may be times where you will still need to call back into the hosting html context.  For then, you'll want to be familiar with two objects HtmlDocument and HtmlPage. 

Both of these objects provide access to the page context hosting your silverlight control.  If you need to seek things in the HTML DOM, you could use the HtmlDocument class.  For example, let's say I need to change the innerHTML property of some <div> element:

using System.Web.Browser;

HtmlDocument doc = HtmlPage.Document;
doc.GetElementById("mydiv").SetProperty("innerHTML", "<b>hello world</b>");

Also, i might want to interact with existing client-side functions, perhaps from client-side frameworks or other library utilities you might have developed on your own.  If I have a function on my page called "foo()" I would invoke it like this:

using System.Web.Browser;

HtmlPage.Window.CreateInstance("foo");

And if I had parameters in a function, like "foo2(theAlert)," I would invoke it like this:

using System.Web.Browser;

HtmlPage.Window.CreateInstance("foo2", new string[] { "tim heuer" });

This may not be the norm with your Silverlight project, but I hope this helps clear some things up!  I am including the "using" statements in my c# samples so you know where in the namespace the class library exists.

  1. 3/10/2008 8:13 AM | # re: Calling Javascript functions from Silverlight 2
    Are there any security restrictions on this? If I want to embed a Silverlight file from another site, could it change the DOM on my page?
  2. 3/10/2008 11:10 AM | # re: Calling Javascript functions from Silverlight 2
    Michael, you can disable HTML access via a parameter on your silverlight host declaration (and the SL app author can check for that via HtmlPage.IsEnabled)
  3. 3/10/2008 5:46 PM | # CreateInstance vs Invoke
    I'm using the same concept to call a JS function from my Silverlight... but I do it using HtmlPage.Window.Invoke... is there any difference?
  4. 3/11/2008 2:20 AM | # re: Calling Javascript functions from Silverlight 2
    Being able to access the DOM from compiled code on the client is really impressive, Silverlight is just amazing.
  5. Gravatar
    3/11/2008 1:56 PM | # re: Calling Javascript functions from Silverlight 2
    How can you do the opposite? How can we call Managed Code functions from JavaScript? The old way that worked in Alpha 1.1 no longer works, and the example on MSDN says to use sender.get_content() and this doesn't work either. Any ideas?
  6. 3/15/2008 11:50 AM | # re: Calling Javascript functions from Silverlight 2
    To call C# from JavaScript:

    1. In Application_Startup() in App.xaml.cs, create the Page object in a variable and before setting the RootVisual to this value, add: HtmlPage.RegisterScriptableObject("Page", page);

    2. In Page.xaml.cs, add the method to be called from JavaScript and decorate it with this attribute: [ScriptableMember]
    public void MyMethod() { ... }

    3. In your HTML page, make sure your ASP.NET Silverlight control has an ID:
    <asp:Silverlight ID="_silverlight" ...

    4. Call the C# method from JavaScript:
    silverlightControl = document.getElementById("_silverlight");
    silverlightControl.Content.Page.MyMethod();
  7. 4/8/2008 8:16 AM | # re: Calling Javascript functions from Silverlight 2
    Don't know if this has changed recently, but I found the import was system.windows.browser, not system.web.browser.

    Cheers
  8. 6/16/2008 10:20 PM | # re: Calling Javascript functions from Silverlight 2
    hey pls I am in bad need for ur help I need to control virtual earth via sliverlight 2 bete 2 if some one know a way thx in advance
  9. 7/19/2008 3:32 PM | # re: Calling Javascript functions from Silverlight 2
    Thanks!
  10. 9/17/2008 3:33 AM | # re: Calling Javascript functions from Silverlight 2
    how do i call .Cs Side function to Source(.aspx)side by JavaScript ?
  11. 9/19/2008 7:17 PM | # re: Calling Javascript functions from Silverlight 2
    Neetesh: you can use MS Ajax library to expose page methods to your client side page.
  12. 9/30/2008 3:06 AM | # re: Calling Javascript functions from Silverlight 2
    plz share the code for accessing the scripts using in google adwords.
  13. 11/7/2008 1:11 AM | # re: Calling Javascript functions from Silverlight 2
    Dear tim,
    I am really sorry that I am asking such a question to a person like you.
    I am trying to access a stackpanel which is in silverlight through javascript,But not being able.
  14. 11/7/2008 10:57 AM | # re: Calling Javascript functions from Silverlight 2
    Jitendra: Send me your project code and I'll take a look at where you are stumbling.
  15. 12/12/2008 2:25 AM | # re: Calling Javascript functions from Silverlight 2
    hi,
    I want to call a client side function on mouse Event. But debugger showing it as wrong code it.Even if I am writing function in aspx code inside script tag its not acknowledged. Tell me how should I call javascript function on event for silverlight object.
  16. 12/12/2008 6:58 AM | # re: Calling Javascript functions from Silverlight 2
  17. 1/20/2009 3:27 AM | # re: Calling Javascript functions from Silverlight 2
    Its very nice
  18. 1/31/2009 11:00 AM | # re: Calling Javascript functions from Silverlight 2
    Thank you - do these calls wait for the dom to be ready (ie. jQuery ready) prior to running - so if you put a call in Page() it will fire when the dom is loaded?

  19. 2/4/2009 8:40 PM | # re: Calling Javascript functions from Silverlight 2
    Steve you wouldn't want to put anything in the constructor to ensure the rest of the Silverlight UI layout is loaded -- do init stuff in a Loaded event handler. That being said, it's possible your Silverlight app starts before the rest of the page content loads (since it is a part of the DOM as well) -- so if you need to rely on the full DOM to be loaded, consider some type of callback from body.onLoad
  20. 6/11/2009 1:53 AM | # re: Calling Javascript functions from Silverlight 2
    hello...i want to do validation in silverlight using javascript..can you plz show me small demo of validation??

 
Please add 1 and 7 and type the answer here:
First time here? You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting! (hide this)