How to programmatically detect Silverlight version

by APIJunkie 24. April 2009 23:39

Two questions that often arise when building Silverlight based websites are:

  A. How do I detect if Silverlight is installed on a visitor's browser?

  B. How do I detect what version of Silverlight is installed on a visitor's browser?

The answer to A is relatively simple since Silverlight.js (the standard Silverlight javaScript include file) contains a function that we can use. The function is called isInstalled and it returns true/false. You can use it the following way to detect if there is any Silverlight version installed:

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

var isSLInstalled = Silverlight.isInstalled(null)

The answer to B is a little more complex since for some reason there is no direct way to get Silverlight's version number. Basically the only documented way to answer this question is to repeatedly call isInstalled with different version numbers until you get the right version.

Example:

Silverlight.isInstalled('3.0')

Silverlight.isInstalled('2.0')

Silverlight.isInstalled('1.0')

etc.

Every time we call isInstalled the function code goes through the same process of trying to create an ActiveX/Plugin object etc. In some parts of the programming world this kind of inefficiency would be labeled as border line heresy. But luckily Since we are normally only interested in Silverlight's major version we can do things a little more efficiently. The function below will only return 0,1,2,3 where 0 stands for no version and 1 to 3 stand for a Silverlight major version.*

//////////////////////////////////////////////////////////////////

// get major Silverlight version

// Return values:

// 0 -> Silverlight not installed (at least not properly).

// 1 -> Silverlight 1 installed

// 2-> Silverlight 2 installed

// 2-> Silverlight 3 installed

//////////////////////////////////////////////////////////////////

getSilverlightVersion = function() {

var SLVersion;

try {  

       try {

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

            if (control.IsVersionSupported("3.0"))                

               SLVersion = 3;

            else

            if (control.IsVersionSupported("2.0"))               

               SLVersion = 2;

            else

               SLVersion = 1;           

            control = null;

      }

      catch (e) {      

                     var plugin = navigator.plugins["Silverlight Plug-In"];

                     if (plugin)

                     {         

                       if (plugin.description === "1.0.30226.2")             

                          SLVersion = 2;

                       else

                          SLVersion = parseInt(plugin.description[0]);

                      }

                      else

                         SLVersion = 0;

      }

}

catch (e) { 

      SLVersion = 0;

}

return SLVersion;

}

-------------

* Note that this code will break with future versions of Silverlight/silverlight.js so it needs to be rechecked when a new version is released.

 

Tags:

Silverlight | How To

Comments

4/25/2009 1:24:25 AM #

trackback

Trackback from DotNetKicks.com

How to programmatically detect Silverlight version

DotNetKicks.com

4/25/2009 8:46:08 PM #

pingback

Pingback from silverlight-travel.com

Silverlight Travel » How to programmatically detect Silverlight version

silverlight-travel.com

4/26/2009 10:09:27 PM #

Jones

Nice post...

Jones United States

5/8/2009 2:39:06 AM #

pingback

Pingback from kintespace.com

the rasx() context  » Blog Archive   » “The $6 Billion Black Hole Implodes” and other links…

kintespace.com

8/31/2009 1:32:39 AM #

pingback

Pingback from ramanisandeep.wordpress.com

How to Programmatically detect Silverlight version? « Ramani Sandeep

ramanisandeep.wordpress.com

1/4/2010 5:56:51 PM #

pingback

Pingback from autocreate.wordpress.com

Silverlight: Nice detection method « delegate () { solve(everything); }

autocreate.wordpress.com

Comments are closed

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon