×

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!

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.


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?
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/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?
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.
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?
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();
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
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
7/19/2008 3:32 PM | # re: Calling Javascript functions from Silverlight 2
Thanks!
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 ?
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.
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.
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.
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.
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.
12/12/2008 6:58 AM | # re: Calling Javascript functions from Silverlight 2
1/20/2009 3:27 AM | # re: Calling Javascript functions from Silverlight 2
Its very nice
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?

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
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??
10/20/2009 5:25 AM | # re: Calling Javascript functions from Silverlight 3
how to call the aspx methods in silverlight3 using C#
10/20/2009 8:58 AM | # re: Calling Javascript functions from Silverlight 2
mahi - as long as the page methods are exposed as public you use the same methods.
11/22/2009 3:55 AM | # re: Calling Javascript functions from Silverlight 3
How to Call Javascript functions from Silverlight 3 ???
Gravatar
12/20/2009 6:18 AM | # re: Calling Javascript functions from Silverlight 2
hi Tim,
Is there a way for silverlight 2 to invoke a javascript function
and receive its return value?
(I could use hidden html fields, but prefer a clean API)
Thanks
2/18/2010 11:23 AM | # re: Calling Javascript functions from Silverlight 2
gil, if you are still looking for answer, heres waht i do.

javascript--
function sendText() {
var oReturnValue = confirm("Do you want to delete?");
return oReturnValue;
}

button click on page.xaml--
bool retValue = (bool)HtmlPage.Window.Invoke("sendText", null);
if (retValue)
{
// your code goes here.
}
Gravatar
2/22/2010 11:19 AM | # re: Calling Javascript functions from Silverlight 2
The Function object permits a function to have methods and properties associated with it. To accomplish this, the function is temporarily considered to an object whenever you wish to invoke a method or read the value of a property.

Note that JavaScript treats the function, itself, as a data type that has a value. To return that value, the function must have a return statement.

When a Function object is created by using the Function constructor, it is evaluated each time. This is not as efficient as the alternative method of declaring a function using the function statement where the code is compiled.

The following example creates a Function object to calculate the average of two numbers, and then displays that average:
6/4/2010 10:50 AM | # re: Calling Javascript functions from Silverlight 2
Help, the name space Web not not exist in the namespace System -- what assembly reference do i need????? to use "RegisterStartupScript"


please include this vital info for nubes, it takes hours to track this stuff down....

:(

6/4/2010 11:29 AM | # re: Calling Javascript functions from Silverlight 2
Tim's video explains it all! To pass a string to javascript from Silverlight:

HtmlPage.Window.Invoke("myJavascriptFunk", new string[] { smashedString });


this works but only for Int, otherwise it will converts the string to an int
HtmlPage.Window.Eval("myJavascriptFunk("+ smashedString +")");
10/21/2010 1:11 PM | # re: Calling Javascript functions from Silverlight 2
Good explanation.

Below link provides good details about the samme

www.a2zmenu.com/.../...ethod-from-Silverlight.aspx
10/23/2010 12:46 AM | # re: Calling Javascript functions from Silverlight 2
Also for calling Silverlight method from JavaScript

www.a2zmenu.com/.../...Method-from-JavaScript.aspx
11/9/2010 9:05 AM | # re: Creating and Calling Javascript functions from Silverlight 4
Hi Tim,
I'm trying to mimic the RegisterClientScriptBlock functionality from Silverlight. I want to build a javascript function, as a string, using say button1's click event and register that to the master page and then use the HtmlPage.Window.CreateInstance("CreatePrinterScript"); to print it. Can you help?
3/6/2012 8:10 AM | # re: Calling Javascript functions from Silverlight 2
I can successfully call a JavaScript function using Silverlight 4 with the HtmlPage.Window.Invoke(). This runs in localhost but when I publish it to the web, the JavaScript fails to execute. I have cleared browser cache to no avail. Any suggestions? Thanks

 
Please add 7 and 3 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.