Dynamic display causes JavaScript bridge to fail in Silverlight 2 - Update

The problem I wrote about earlier talking about the JavaScript bridge failing when the plug-in is in a div that has it's display set to hidden, is still in Silverlight 2 RTW. But I figured out why. When the plug-in is in a hidden div, the bridge doesn't work, and showing the div from JavaScript and calling the plug-in directly after isn't working either. I tried doing the following, without success:
[code:js]
var hostDiv = document.getElementById("silverlightControlHost");
var plugin = hostDiv.childNodes(0);
hostDiv.className = "hostDivVisible";
plugin.content.ScriptableObjectRef.CallMethod();
[/code]

So why is it failing. My gues is that setting the className will queue up that request making the actual visibility change after the method has run. That means that the DIV is actually still not visible when the call is made to the Silverlight application. So to solve this, you have to get the browser to hold off calling that method until the DIV has actually been shown. You can do this by queuing up your call to the method as well.
[code:js]
var hostDiv = document.getElementById("silverlightControlHost");
var plugin = hostDiv.childNodes(0);
hostDiv.className = "hostDivVisible";
window.setTimeout("document.getElementById(\"silverlightControlHost\").childNodes(0).content.ScriptableObjectRef.CallMethod();", 0);
[/code]

At least this workaround works in IE7. I'm currently too busy to check it out in different browsers, but I guess the result should be the same.

Comments are closed