Silverlight: We've Got Window.Eval()!! Yay!!

by Jon Davis 9. March 2008 23:52

W00t!! The window.eval() function is something I've been wanting in Silverlight forever!! Looks like it's here now, finally, in Silverlight v2!

// App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e)
    // Load the main control      // generated
    this.RootVisual = new Page(); // generated
    // added ..
    string html = HtmlPage.Window.Eval("document.documentElement.innerHTML").ToString();
    HtmlPage.Window.Alert(html); // worked!

Now lemme try some jQuery ..

<!-- HTML: -->
<script type="text/javascript" language="javascript">
    function showAlert(el) {
        alert("Alert from jQuery introspection of $(" + el.tagName + ").html() ..\n\n"
            + $(el).html());


// App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e)
    // Load the main control       // generated
    this.RootVisual = new Page();  // generated
    object bodyObject = HtmlPage.Window.Eval("document.body");
    HtmlPage.Window.Invoke("showAlert", bodyObject);

W00t! It works!! Now lemme try some plugin interop using only outer passing...

<!-- HTML -->
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
    width="550" height="400" id="tweenMov">
    <!-- first random swf I found -->
    <param name="movie" value="">
    <param name="quality" value="high">
    <param name="bgcolor" value="#000000">
    <embed src="/support/flash/ts/documents/myFlashMovie.swf"
        quality=high bgcolor=#FFFFFF width="550" height="400"
        name="tweenMovie" id="smurfsMovie align="" type="application/x-shockwave-flash"
// App.xaml.cs:
object movieObject = HtmlPage.Window.Eval("tweenMovie");

Hmm, putting a breakpoint on this and introspecting movieObject, it's still an HtmlElement with nothing much associated with it.

I can still pass it around as an object reference ...

Initial jQuery test:

<!-- HTML  <script>: -->
window.onload = function() {
    var movieObj = $("#tweenMov")[0];
    alert("bgcolor=" + movieObj.bgcolor + "\n"
        + "quality=" + movieObj.quality);

.. and then try same test after passing to Silverlight and having Silverlight pass it back out again ..

<!-- HTML <script>: -->
function showParams(movieObj, fromMsg) {
    alert(fromMsg + "\n\nbgcolor=" + movieObj.bgcolor + "\n"
        + "quality=" + movieObj.quality);


// App.xaml.cs:
object movieObject = HtmlPage.Window.Eval("tweenMov");
HtmlPage.Window.Invoke("showParams", movieObject, "From Silverlight:");

Worked! Tested in Firefox, too, but it failed; apparently, it choked on the Flash markup before the other stuff had a chance, and I'm too tired to clean it up.

Now let's see if I can declare Javascript functions on the fly so that I can use browser scripting to perform cross-plugin communications without prior scripting in markup:

// App.xaml.cs:
object val = HtmlPage.Window.Eval("function showHelloWorld() {"
    + "     alert(\"Hello world! This is Silverlight talkin!\");"
    + "     return \"Yay!\";"
    + "}"
    + "showHelloWorld();");

It works. Yay!! Can I do the same to elaborately introspect the Flash plug-in?

object val = HtmlPage.Window.Eval("function getFlashBgColor() {"
    + "return $(\"#tweenMov\")[0].bgcolor;"
    + "}"
    + "getFlashBgColor();");
HtmlPage.Window.Alert("Flash background color: " + val.ToString());

Looks like I got my wish! Now I can use the CLR to control DHTML through the browser script runtime, which alone knows the full extent of its DOM. Silverlight is browser script runtime friendly!

kick it on

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Computers and Internet | Software Development | Web Development


Add comment

(Will show your Gravatar icon)  

  Country flag

  • Comment
  • Preview


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  June 2021  >>

View posts in large calendar