Udostępnij za pośrednictwem


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.

Comments

  • Anonymous
    May 07, 2008
    PingBack from http://explorer.wawblog.info/?p=4249

  • Anonymous
    May 07, 2008
    Another limitation of the isSilverlightInstalled function is that it is non-portable. What if the user has a Silverlight plugin installed but is running another browser than Internet Explorer, Firefox, or Safari? Lets speculate that the user is running a browser that doesn't have the navigator.plugins object or the ActiveXObject class, then isSilverlightInstalled will return false, even though the plugin is installed. The chances for this is rather slim, but still the test is non-portable. Depending on how the isSilverlightInstalled function will be used, it may or may not matter. If the function is only used for calling the Auto Start method it shouldn't matter.

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

  • 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