Microsoft Developer Network > Forums Home > Gadgets Forums > Web Gadget Development > How to differentiate between Live.com, Spaces, and Gallery
Ask a questionAsk a question
 

General DiscussionHow to differentiate between Live.com, Spaces, and Gallery

  • Tuesday, April 03, 2007 3:44 AMToddOs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I was recently asked to submit a post on how I differentiate between Live.com, Spaces, and Gallery in some of my web gadgets (for example, Hit Counter, a Spaces-only gadget, degrades gracefully on Live.com and Gallery).  In order to most effectively illustrate the concepts, I wrote a simple gadget that prints where it's running ("Live", "Spaces", or "Gallery"), and in what mode ("author" or "view").  You can find the gadget code at http://www.daishar.com/gadgets/whereami/gadget.xml and can see it in action from Donavon's gadget test page (the Spaces frames don't claim to be running on Spaces, but that's a deficiency in the test page that I'll mention later).  With that out of the way, let me explain the core concepts.

     

    First, just to make things a bit easier, I define an enum for the possible locations:

     

    ToddOs.WhereAmI.Location = Web.Enum.create("Live", "Gallery", "Spaces");

     

    The meat of the code is in two functions.  The first one checks if the gadget is running on Spaces:

     

        function CheckSpaces()

        {

            // are we running on Spaces?  Ugly!

            var loc = window.location.toString()

            if (loc.indexOf("spaces.start.com") >= 0)

            {

                m_isSpaces = ToddOs.WhereAmI.Location.Spaces;

                // get the Space name

                m_spacesName = loc.substring(loc.indexOf("host=") + 5, loc.indexOf(".spaces.live.com"));

            }

            else

            {

                // Assume we're on Live for now.  We will figure out if we're on Gallery later

                m_location = ToddOs.WhereAmI.Location.Live;

            }

        }

     

    Update:  This code uses the URL of the gadget iframe to determine if it's running Spaces.  The Spaces iframe is http://spaces.start.com, which is different from the http://gadgets.start.com domain used by live.com.  Previously I relied on the module.getId() function, but recent changes to Spaces broke that functionality.  This approach should be more robust in the long run.

     

    After CheckSpaces() has been called, you know whether or not you're on Spaces, and if so you also have a unique name for the Space on which you're running (a Spaces url looks like "spacename.spaces.live.com", where "spacename" is unique to each space and can be used as a unique identifier -- this is how I identify Spaces for Hit Counter).  You'll notice that we still don't know if we're running on Gallery yet, and just make the assumption that we must be on Live.com if we're not on Spaces.  That's where the next function comes in, which has a two-fold purpose:

     

        function CheckMode()

        {

            m_mode = Web.Gadget.Mode.author;

     

            if (m_module.getMode)

            {

                // Live.com and Spaces define module.getMode(), so we'll use that.

                // Spaces does *not* define p_args.mode

                m_mode = m_module.getMode();

            }

            else if (p_args.mode)

            {

                // Gallery's preview doesn't define module.getMode(), but does define p_args.mode

                m_location = ToddOs.WhereAmI.Location.Gallery;

                m_mode = p_args.mode;

            }

            else

            {

                m_mode = "unknown";

            }

        }

     

    The CheckMode() function figures out what mode the gadget is running in ("author" vs. "view"), and also figures out if the gadget is running on Gallery.  The trick is that Spaces and Live.com define the method getMode() on the Module object, while Gallery doesn't.  Since we already know whether or not we're on Spaces, we can further use this to differentiate between Live.com and Gallery.  Gallery is the odd man out, defining mode as a variable on p_args rather than as a method on Module.  Live.com also defines p_args.mode, but Spaces does not, so you could use that as an alternate method for determining Spaces-ness.  Even if you don't care if your gadget is running on Gallery vs. Live.com,  you still should make this CheckMode() call because using Module.getMode() on Gallery will throw an exception.  That's bad behavior that you want to avoid, because it means that potential users can't preview your gadget on Gallery before adding it to their Live.com or Spaces pages.  A user who can't preview is much less likely to add your gadget than one who can, so always make sure your gadget works on Gallery.

     

    The remainder of the code is trivial.  The initialize() function simply calls CheckSpaces() and CheckMode() before doing anything else, and in the case of the test gadget will output a string containing the location information.  The dispose() function is empty because there's really nothing worth disposing here.

     

    Hopefully this will help some of you build better gadgets that work well across all of the different Web gadget environments.  Feel free to use this code (you'll probably want to put it in your own namespace).

     

    Code is provided as-is.  I've tested it in the context of my sample gadget, and I've used variations of this code in multiple gadgets.  However that's still no guarantee that it will work for you, or even that it will continue working as Spaces, Live.com, and Gallery are continually improving and some of the assumptions here may no longer hold (specifically, I expect Gallery will eventually support Module.getMode(), leaving no easy way to differentiate between Gallery and Live.com).  Use at your own risk.  Don't claim this code as your own.  Other legal mumbo jumbo.  Etc.

All Replies

  • Tuesday, April 03, 2007 5:14 PMnguyentanbao Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks ToddOs,

     

    I noticed there's one attribute: onDashboard. What is it? Can it be applied in detecting the environment the gadget is running on?

  • Wednesday, May 02, 2007 3:24 AMToddOs Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Bumping since there have been some changes to Spaces recently.  Specifically, the module ID format has changed, breaking my original code.  I've updated the original post to reflect my new approach at determining Spaces-ness.