Determining if Silverlight is installed using Javascript

In my recent post entitled Automatically Start Silverlight on Install I discussed a method for starting Silverlight once it is installed without any user interaction. The method discussed in that post relied on Silverlight.js, the helper file used for Silverlight 1.0 instantiation. Since Silverlight 2 predominantly uses <object> tags for instantiation, I want to show how to get the same behavior without the use of Silverlight.js. As a first step, I will talk about determining if Silverlight is installed without using Silverlight.js.

As you probably guessed, the model for determining if Silverlight is installed differs between IE and Firefox/Safari. I will discuss the models separately and then provide a unified method that can answer the question "Is Silverlight installed?".

Internet Explorer
IE sees Silverlight as an ActiveX control and allows us to use standard ActiveX patterns to determine if the control is installed. Specifically, we will ask IE to create a Silverlight instance for us. This is accomplished by the following call:

new ActiveXObject('AgControl.AgControl');

If Silverlight is installed then this call will return an instance of a Silverlight control. Otherwise, the call will throw an exception which tells us that AgControl.AgControl is not defined. With this in mind, we can now write a method which will tell us if Silverlight is installed in IE:

function isSilverlightInstalledIE()

{

    var isSilverlightInstalled = false;

   

    try

    {

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

        //this line is only reached if Silverlight is installed

        isSilverlightInstalled = true;

    }

    catch (e)

    {

        //if Silverlight is not installed, an exception will be

        //thrown. We can ignore this exception in this method.

    }

    return isSilverlightInstalled;

}

Firefox and Safari
Firefox and Safari see Silverlight as a plugin. Therefore, we can use the browser DOM's navigator.plugins array to determine if Silverlight is installed as so:

function isSilverlightInstalledFFSafari()

{

    var isSilverlightInstalled = false;

   

    try

    {

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

        {

            //this block is only reached if Silverlight is

            //installed

            isSilverlightInstalled = true;

        }

    }

    catch (e)

    {

        //if the browser does not support the plugins array

        //then an exception will be thrown. We can ignore

        //the exception in this method.

    }

    return isSilverlightInstalled;

}

Combined
Finally, we can combine the above methods into a single isSilverlightInstalled() function:

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;

}

Limitations
The isSilverlightInstalled function does not tell us anything about the installed version. It only tells us if some version of Silverlight is installed.

 

The combined isSilverlightInstalled function gives us the same information as Silverlight.isInstalled in the Auto Start sample. In my next post, I will merge isSilverlightInstalled, <object> instantiation, and the Auto Start sample to show how you can use the Auto Start method with Silverlight 2.