[OpenLayers-Users] Another Dumb Question?

Christopher Schmidt crschmidt at metacarta.com
Mon Sep 1 17:40:10 EDT 2008


On Mon, Sep 01, 2008 at 12:41:52PM -0600, Bill Thoen wrote:
> In the example 
> http://openlayers.org/dev/examples/custom-control-point.html, the line 
> that says:
> 
> map = new OpenLayers.Map( $('map') );
> 
> I suppose this must return a reference to a div named 'map' but why wrap 
> it in such a weird function? Wouldn't
> 
> map = new OpenLayers.Map( "map" );
> 
> work just as well?

Yes; I generally do the element finding at the application level,
personally, but there is no specific reason for that, so long as you
ensure that IDs are unique.

> I'm still pretty new to JavaScript and OpenLayers but I can't find a 
> reference to the function $()  in my 5 lb JavaScript book (it's amazing 
> what's NOT in the Rhino book!) 

depending on the version of the Rhino book, 'age' might be the reason
for this; a coworker has the v4 Rhino book, and it definitely felt 'old'
when I grabbed it. (I've never looked at any Javascript reference in my
work before that.)

However, in this case, the reason is "$ is OpenLayers, not Javascript."
:)



> nor can I find it in the OpenLayers docs. 

/**
 * Function: getElement
 * This is the old $() from prototype
 */

(Not great docs, but that's on OpenLayers.Util.getElement.)

> And Google seems to have a mental block against searching for anything 
> that looks like punctuation even if I try "$()".

Yep, probably true. Most search engines probably do.

> I'm especially curious because I also saw a reference to "library 
> dependence on $" in the recent discussion about OpenLayers and jQuery, 
> and don't know what it means.
> 
> So would somebody kindly clue me in on this one too?

"In the beginning..."

OpenLayers was based on the Prototype Javascript library. As time went
on, it became clear that Prototype provided not a whole lot of what we
wanted, and did provide a lot that we *didn't* want: It was a large
library (at the time, consisting of 50% of the code in OL) when we used
only a tiny portion. In addition, the portion that we did use has shrunk
over the years as we have learned that Prototype doesn't really do what
we want: Most recently, for example, the "Ajax.Request" code that was
originally from Prototype has been replaced with a differnet
XMLHttpRequest library.

At some point, we tore out the guts of Prototype, and just left in the
things that we used from it. In Prototype, the "$" function is a
shortcut for "getElementById" (sort of) that was shorter to type,
because prototype used it a lot.

At some point, we realized that having this "$" in global namespace was
conflicting with other libraries, and we changed OpenLayers *internally*
to always use OpenLayers.Util.getElement instead. 

However, since we had maintained the "$" into the 2.x branch, we wanted
to maintain this function going forward until the 3.x series. It's there
merely for backwards compatibility, and we try to only define it if it's
not already defined by another library.

So:
 * You shouldn't use $, you should use OpenLayers.Util.getElement
 * getElement returns an element, based on either:
    * the input being an element
    * the input being the id of an element
    * the input being an array of either of the above, in which case
      it returns an array of elements.
 * No internal OL code uses "$", so redefining it to do whatever you
     want can not hurt the functioning of the OpenLayers library (but can
     hurt your application code if you just copy the current examples)
 * $ is a shortcut used for backwards compatibility
 * If you include a library like Prototype before OL, it will try to use
     prototype's "$" instead
 * If you run into problems, feel free to ask about it on the mailing
     list.
 * The examples which use "$" should probably use
     OpenLayers.Util.getElement instead.

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list