Automatically Start Silverlight on Install - Part 1

The last step of the Silverlight installer is to ask the user to restart or refresh their browser. This approach forces the browser to re-evaluate the page's Silverlight instantiation logic once the control is installed. In the case of non-IE browsers, the restart also forces the browser to refresh its cache of known controls.

While this experience is sufficient for most websites, it can be disruptive to sites which want to provide a managed end-to-end user experience. Fortunately, through the use of some simple javascript, these websites can detect when Silverlight installation completes and can launch their Silverlight content without any further user interaction.

Prerequisites

 To get started, we need several logical components:

  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.

For the first component we can use the commonly available setTimeout and setInterval methods. For the purposes of this post I will use setTimeout since it doesn't require us to explicitly cancel future executions. You should evaluate both methods and determine which works best for your scenario.

The second component is also rather easy. The Silverlight.js helper file, available in the Silverlight SDK, provides a method named isInstalled which answers this very question.

Finally, what action should the page take when it detects that Silverlight is installed? The simplest answer is to refresh itself. This will enable the default instantiation logic to render the Silverlight application without any additional modifications.

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 ( Silverlight.isInstalled("1.0") )

            {

                //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 ( !Silverlight.isInstalled("1.0") )

            {

                //Silverlight is not installed. Try to refresh

                //the page when it is installed.

                WaitForInstallCompletion();

            }

        }

    </script>

</head>

<body onload="onLoad()">

    <!—-

These references should be changed to point at the corresponding files in your web application

    -->

    <script type="text/javascript" src="Silverlight.js"></script>

    <script type="text/javascript" src="CreateSilverlight.js"></script>

    <div id="AgControl1Host">

        <script type="text/javascript">

            createSilverlight();

        </script>

    </div>

</body>
</html>

 

Limitations
This approach is limited to new installations of Silverlight. Websites which require a newer version of Silverlight than what is already installed on the user's machine will still require a restart. Fortunately, Silverlight's automatic updater makes new version requirements a transient problem.

Conclusion
The Silverlight installation process provides a good user experience with minimal requirements for the web author. The script solution presented in this post builds on the default experience by helping websites take more control their end-to-end flow.