Sure, it's just a combination of some other JavaScript features that may
look more familiar if we take them one by one:
// Use an object literal to create an object
// with two properties. Each property has
// a name and a value.
var images = {
'/services': 'one-image.png',
'/about-us': 'another-image.png'
};
// Select one of the properties from the
// object by name, and get its value. If
// not found, the value is undefined.
// (That doesn't mean an unpredictible
// value, it means the specific value in
// JavaScript known as "undefined".)
var img = images[location.pathname];
// Select a default image if img is undefined.
img = img || 'default-image.png';
// Or another way to do that last statement
// (means exactly the same thing):
if( ! img )
img = 'default-image.png';
-Mike
> From: Wonder95
>
> Could you explain that construct? I'm no JS expert, and I
> haven't seen it before.
>
> Thanks.
> > If you want to do it in JavaScript, you don't need jQuery, regular
> > expressions, or indexOf. window.location (or just location) has
> > several properties that give you different pieces of the URL.
> > location.pathname is the one you want here. Note that it
> includes the leading slash. For example:
> >
> > var img = {
> > '/services': 'one-image.png',
> > '/about-us': 'another-image.png'
> > }[location.pathname] || 'default-image.png';