Automatically Start Silverlight on Install - Part 2

Silverlight 2 changes the default instantiation model of the Silverlight control from Silverlight.js to the <object> tag. This change makes it seem that Automatically Starting Silverlight on Install requires rewriting our instantiation code and moving back to the Silverlight.js model. Fortunately, you can get the same auto-restart behavior using the <object> model.

My previous post on this subject called out that you need three things to build the auto-restart experience:

  1. A method for asking the browser if Silverlight is installed repeatedly, but without locking up the site.
  2. A way to tell that Silverlight is installed.
  3. An action to take once Silverlight is available.

Only the second, a way to tell that Silverlight is installed, was provided by Silverlight.js. Everything else in that model is agnostic to the instantiation model. Since we can determine if Silverlight is installed using javascript without Silverlight.js we can rewrite the previous example to use the <object> instantiation method.

Background

These posts provide background information that will explain how/why this works:

Implementation

<html>

<head>

    <title>Automatically Start Silverlight on Install</title>

    <script type="text/javascript">

        WaitForInstallCompletion = function()

        {

            try

            {

                //This forces Firefox/Safari to refresh their

                //list of known plugins.

                navigator.plugins.refresh();

            }

            catch(e)

            {

                //IE does not support the method, so an

                //exception will be thrown.

            }

            if ( isSilverlightInstalled() )

            {

                //Silverlight is installed. Refresh the page.

                window.location.reload(false);

            }

            else

            {

                //Wait 3 seconds and try again

                setTimeout(WaitForInstallCompletion, 3000);

            }

        };

        onLoad = function()

        {

            //This only works if we are performing a clean install,

            //not an upgrade.

            if ( !isSilverlightInstalled() )

            {

                //Silverlight is not installed. Try to refresh

                //the page when it is installed.

                WaitForInstallCompletion();

            }

        }

        function isSilverlightInstalled()

        {

            var isSilverlightInstalled = false;

           

            try

            {

                //check on IE

                try

                {

                    var slControl = new ActiveXObject('AgControl.AgControl');

                    isSilverlightInstalled = true;

                }

                catch (e)

                {

                    //either not installed or not IE. Check Firefox

                    if ( navigator.plugins["Silverlight Plug-In"] )

                    {

                        isSilverlightInstalled = true;

                    }

                }

            }

            catch (e)

            {

                //we don't want to leak exceptions. However, you may want

                //to add exception tracking code here.

            }

            return isSilverlightInstalled;

        }

    </script>

</head>

<body onload="onLoad()">

  <div id='errorLocation' style="font-size: small;color: Gray;"></div>

  <div id="silverlightControlHost">

   

    <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="320" height="320">

      <param name="source" value="https://silverlight.net/samples/2b1/DynamicLanguageSDK/samples/clock-py/app.xap"/>

      <param name="background" value="#00000000" />

      <param name="windowless" value="true" />

     

      <a href="https://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">

          <img src="https://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>

      </a>

    </object>

    <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>

  </div>

</body>

</html>

Explanation

As you can see, I replaced the call to Silverlight.isInstalled("1.0") with a call to isSilverlightInstalled(); Since we don't care about the version of Silverlight, this function is sufficient. Next, I replaced the call to CreateSilverlight() and thus Silveright.js with a Silverlight <object> element. And that's it. The rest of this example works just like the Silverlight.js implementation.