Javascript – Silverlight 3 onload event binding multiple instances

javascriptonloadsilverlightsilverlight-3.0

I have two silverlight instances embedded in a page like follows (content simplfied to relevant details only):-

<object id="SL1" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
   <param name="source" value="/clientBin/Common.xap"></param>
   <param name="minRuntimeVersion" value="3.0.40624.0"></param>
   <param name="windowless" value="True"></param>
   <param name="onload" value="silverlight_onload"></param>
   <param name="initParams" value="some configuration here"></param>
 </object>

<object id="SL2" data="data:application/x-silverlight-2," type="application/x-silverlight-2">
   <param name="source" value="/clientBin/Common.xap"></param>
   <param name="minRuntimeVersion" value="3.0.40624.0"></param>
   <param name="windowless" value="True"></param>
   <param name="onload" value="silverlight_onload"></param>
   <param name="initParams" value="a different configuration here"></param>
 </object>

As you can see both objects point at the same xap but will have different content in the initParams.

Note, however, that the onload param points to the same function name. This particular part of this question is invariant.

Elsewhere in javascript at the global level I have:-

function silverlight_onload(sender, eventargs)
{
  //Magic pixie dust here
}

The goal is to take the sender and determine which of the object elements is the source of the event. The straight-forward (but for the sake of this question unacceptable) answer would be to use two different functions. I'm look for something that does not require a new function be added to the global namespace per SL object.

Bonus, kudos goes to anyone who can tell me why SL couldn't get a little more assistance from the host browser and handle the onload parameter more like other element events like onlick. For example ask the browser to eval("function() {" + onload + "}" and attach the return function to the onload event.

Best Answer

Is your goal to get the initparams in the javascript and then do something with it? If so you can do:

sender.getHost().InitParams

the getHost() returns the actual plugin object see msdn page for more details. I don't know of a way to get the object's ID unless you set it as an initparam.