Click to See Complete Forum and Search --> : the all powerful '$'


sneakyimp
04-03-2009, 01:21 AM
I am seeing some whack Javascript in a site I'm dealing with. There are many many references to $ and I cannot seem to find any place where this might have been defined as a legit variable:

e.g.,
$("#some_html_element_id").html('<p>A bunch of html</p>');

and this:
var all_relevant_items = $("#xyz_selector .style1");
all_relevant_items.filter("[attr!='" + this.prop + "']").each(function(){
$(this).activate("onDeactivate");
});


Anyone got any tips on how I might locate the definition of this mysterious object and shed some light on this? What the heck is this code doing?

NogDog
04-03-2009, 02:16 AM
In JavaScript the dollar sign can actually be used as an identifier, such as a variable or function name, so presumably somewhere in that script there is a function definition named simply "$". It could have as easily been called "a", "b", or even "_". In any of those cases, it's a lousy naming convention unless you are purposely trying to achieve some source code obfuscation.

sneakyimp
04-03-2009, 04:18 AM
After a bit of agonizing, I realized it's JQuery. I am not certain, but I believe I could replace every instance of $ with 'JQuery' and it would still work.

Thanks for your response. This was driving me nuts at first. I'm starting to realize that JQuery can be quite potent though. It takes a certain mindframe to understand it, but it seems to be pretty effective shorthand.

Weedpacket
04-03-2009, 06:14 AM
I am not certain, but I believe I could replace every instance of $ with 'JQuery' and it would still work.I think that's right: they introduced that because it conflicted with prototype's definition of "$" (I recall having to deal with the conflict once).