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.