views:

150

answers:

2

I have a web application containing a silverlight control. When the specific page is accessed that contains the silverlight control a javascript is executed and if this script is executed too early, the script crashes the javascript engine in IE, not even a try/catch can handle the error.

Simple javascript

silverlightHtmlElement.Content.SilverlightApplication.SilverlightMethod();

If the silverlight control element hasn't been fully loaded the script crashes, and not only the script, no further javascript is executed on the page at all.

A simple "if (silverlightHtmlElement.Content.SilverlightApplication)" to check if the its accessible is enough for a crash.

The problem is that I can't execute the script later on the page. Anyone else seen this before?

A: 

You could write some Silverlight code that executes a javascript function after it has finished initalizing. This way your silverlight app can notify the web page when it has finished loading.

HtmlPage.Window.Invoke("myJsFunction", null);
Jeremiah Morrill
A: 

you need to make sure that the code is not executed until the control is active/loaded, add this param to your silverlight object tag:

<param name="onLoad" value="pluginLoaded" />

then define a javascript function on the page like this

var app= null;
function pluginLoaded(sender, args) {
    app= sender.getHost();
}

then you can just add your call to the method to the loaded event handler or query the app variable for null to see if its ready.

luke