Skip to main content

Microsoft Silverlight

how does this javascript detect silverlight?RSS Feed

(0)

BrownWarrior
BrownWar...

Member

Member

18 points

22 Posts

how does this javascript detect silverlight?

Hi,

how does this javascript detect silverlight?

 

 

if ((navigator.userAgent.indexOf('Windows') != -1) && (navigator.appVersion.indexOf('MSIE') != -1) )
        {
            try
            {
                var AgControl = new ActiveXObject("AgControl.AgControl");            
                agVersion = AgControl.settings.version;                
                AgControl = null;
                
            }
            catch (e)
            {
                agVersion = -1;
            }
        }
        return agVersion;

 

 i understand the first bit is detecting IE and at the end agVersion will be something like 1.0.2 or 0.9.0 or -1 or something and i understand that it is attempting to create a silverlight control and if it succeeds then silverlight exists and if it doesn't succeed then it doesn't exist but it is this bit i don't understand.....

 

 new ActiveXObject("AgControl.AgControl");
 

 

particularly the AgControl.AgControl

 

is it naming the ActiveXObject ... AgControl.AgControl or is it specifing the type of ActiveXObject as an AgControl?

 

and why is it written twice, AgControl.AgControl as opposed to just AgControl?

 

and finally the big question does this method of detecting silverlight in IE  still work for the latest version of silverlight? if not what needs to be changed?

 

thanks,

 

Duncan 

 

swildermuth
swildermuth

Star

Star

8320 points

1,546 Posts

Re: how does this javascript detect silverlight?

This code attempts to create the ActiveX object for IE on Windows (the paths are different for FF and other browsers).  If it throws an exception, it says that the agVersion = -1 which means it isn't installed.

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 3 Workshop
Miami, FL: Oct 12-14th
Portlant, OR: Dec 2-4th
Atlanta, GA: Dec 7-9th
http://silverlight-tour.com

BrownWarrior
BrownWar...

Member

Member

18 points

22 Posts

Re: how does this javascript detect silverlight?

Thanks for your reply, but as i said in my post i already understand this much.

 what i need explaining is the "AgControl.AgControl" is this naming the active X object? or is it defining it as a silverlight control? and why is it put twice AgControl.AgControl instead of just AgControl?

 

and the big question, does this code still work with the new version of silverlight? and if not how does it need to be modified?

 

thanks,

gwynn
gwynn

Member

Member

178 points

30 Posts

Re: how does this javascript detect silverlight?

Hi Duncan,

When Microsoft created Silverlight they created an application called AgControl and then created an ActiveX control inside that application called AgControl. That's why it's listed twice. The first bit refers to the name of the application and the second bit refers to the name of the control.

When new ActiveXObject("AgControl.AgControl"); is called, you're asking the browser to find the application called AgControl and then try create an object of type AgControl.

e.g. to create an Excel Spreadsheet you would do something like the following. Excel is the application and Sheet is the control name

 

new ActiveXObject("Excel.Sheet"); 

If Excel is installed and it has a Sheet control then you would get back an object, if not then you would get an exception. That's how you can tell if Excel is installed. Same deal with Silverlight.

My experience so far has been with the Silverlight 1.1 Alpha release. In that release there is a file called Silverlight.js that contains all the code needed to create the Silverlight control. This will simplify your code as you don't have to create the Silverlight control yourself - you just ask it to create itself. That way you don't have to worry about what browser you're running and what OS is installed. (I believe that the code you posted will only work in a windows environment since I don't think the other OSes will know what new ActiveXObject means so it's best to use the JavaScript file that's provided rather than write it yourself.)

If you include the Silverlight.js file in your page then you'll be able to create the Silverlight control and tell it what xaml it's going to show with the following code:

    Silverlight.createObjectEx({
        source: "Page.xaml",
        parentElement: document.getElementById("SilverlightControlHost"),
        id: "SilverlightControl",
        properties: {
            width: "100%",
            height: "100%",
            version: "1.1",
            enableHtmlAccess: "true"
        },
        events: {}
    });
 
 
Inside Silverlight.js there's a createObjectEx function that will detect what OS and browser you're running and then create the Silverlight control using whatever syntax  is appropriate for the browser and OS. 

Cheers,
Gwynn
http://blog.silverspud.com/

--------------------------
Gwynn Kruger
http://blog.silverspud.com
--------------------------
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities