共用方式為


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.

Comments

  • Anonymous
    May 02, 2008
    A few weeks ago I noticed a few changes in browsers behaviors&#8230;&#160; 1) My IE&#160; 7.0.6000&#160;

  • Anonymous
    May 03, 2008
    Posting these links on Shutdown day is just a coincidence. I&#39;m not a person who is unfriendly to

  • Anonymous
    May 05, 2008
    I thought that the Silverlight 2.0 instantiation defaulted to use the OBJECT tag instead of the Silverlight.js. Using Silverlight.js to instantiate Silverlight is non-portable, and restricts Silverlight to be deployed only on platforms sanctioned by Microsoft.

  • Anonymous
    May 06, 2008
    I have the same question as dalle. I understand how this process above works with Silverlight 1.0, but how does this translate into Silverlight 2.0? I am using the <object> tag to indicate whether Silverlight 2 Beta 1 is installed. How can I use the process described above with the <object> tag method?

  • Anonymous
    May 07, 2008
    In my recent post entitled Automatically Start Silverlight on Install I discussed a method for starting

  • Anonymous
    May 30, 2008
    Silverlight 2 changes the default instantiation model of the Silverlight control from Silverlight.js

  • Anonymous
    June 13, 2008
    The forthcoming Firefox 3 release will break some Silverlight 1.0 installation experience on web sites

  • Anonymous
    June 13, 2008
    The comment has been removed

  • Anonymous
    June 16, 2008
    De nieuwe Silverlight 2 SDK heeft een vernieuwde Silverlight.js die problemen oplost rond installatie

  • Anonymous
    June 20, 2008
    Al principio de esta semana se hizo pública la versión de 3 de Mozilla Firefox , el navegador Web estrella