views:

943

answers:

5

In the flowing javascript code there is one $(window). What does it mean???

$(window).bind('load', function() {
$('img.protect').protectImage();
});
+14  A: 

That is most likely jQuery code (more precisely, JavaScript using the jQuery library).

The $ represents the jQuery Function, and is actually a shorthand alias for jQuery. (Unlike in most languages, the $ symbol is not reserved, and may be used as a variable name.) It is typically used as a selector (i.e. a function that returns a set of elements found in the DOM).

Noldorin
it's prototype's selector too ..
Mercer Traieste
You should add that $ is just like x, it could be anything you assign to it
hasen j
+1  A: 

Probably JQuery.

link text

Vnuk
+4  A: 

the "$()" is the shorthand version of jQuery() used in jQuery Library

chchrist
+53  A: 

Your snippet of code looks like it's referencing methods from one of the popular JavaScript libraries (jQuery, ProtoType, mooTools, and so on).

There's nothing mysterious about the use of "$" in JavaScript. $ is simply a valid JavaScript identifier.

JavaScript allows upper and lower letters, numbers, and $ and _. The $ was intended to be used for machine-generated variables (such as $0001).

Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it. In that case you use "jQuery" instead of "$". In fact, "$" is just a shortcut for "jQuery".

Nosredna
This is a much better answer than the accepted one. +1
Paolo Bergantino
@Paolo Bergantin , yup i think so...
coderex
@Paolo: Not sure exactly what you mean. (Mine previously the accepted one, I assume.) Well, I thought mine was fully correct at least. This includes a bit of ancillary information, but I'm not sure it justifies the "much"...
Noldorin
@Noldorin: ancillary information is a good thing. Your question had made absolutely no mention of the reason $ was used, of the reason why it could be used, and of the fact jQuery is just one of many libraries that use it. Having it as an accepted answer means it is the best possible answer for a question, and it wasn't.
Paolo Bergantino
+1 It’s a valid JavaScript identifier first and often used by JavaScript frameworks as an alias second.
Gumbo
@Paolo: Often, yes. There was however no indication the OP wanted to know about this. A direct answer is also a good thing. :) Saying that, I would still agree this is a better answer. My only quibble was with the emphasis you were placing on a specific point.
Noldorin
I thought Noldorin's answer was fine. :-) I'm going to add a sentence right up front to say that this code looks like it's referencing methods from one of the popular JS libraries.
Nosredna
@Noldorin: The direct question was in the title, and the title says "what is the meaning of $ sign in javascript" - answering that question directly, it is extremely relevant that it is simply a valid identifier that happens to be used by libraries. Your answer is correct, obviously, but only taking the body into account.
Paolo Bergantino
BTW, I provided a little history behind the $ function in my post here: http://stackoverflow.com/questions/1122690/jquery-and-questions/1122740#1122740
SolutionYogi
Great stuff, Yogi.
Nosredna
+2  A: 

From another answer:

A little history

Remember, there is nothing inherently special about '$'. It is a variable name just like any other. In earlier days, people used to write code using document.getElementById. Because JavaScript is case-sensitive, it was normal to make mistake while writing document.getElementById. Should I capital 'b' of 'by'? Should I capital 'i' of Id? You get the drift. Because functions are first class citizens in JavaScript, you can always do this

var $ = document.getElementById; //freedom from document.getElementById!

[EDIT: Looks like in Firefox 3 and Google Chrome, you can't make alias so easily. In IE6 and Firefox2, above technique still works.]

When Prototype library arrived, they named their function, which gets the DOM elements, as '$' to save on typing/readability [When writing JS code, most of the time you start with selecting some DOM elements]. Almost all the JavaScript libraries copied this idea. Prototype also introduced $$ function to select elements using CSS selector.

jQuery not only adapted the '$ function', but expanded to make it accept all kind of 'selectors' to get the elements you want. Now, if you are already using Prototype in your project and wanted to include jQuery, you will be in problem as '$' could either refer to Prototype's implementation OR jQuery's implementation. That's why jQuery has the option of noConflict so that you can include jQuery in your project which uses Prototype and slowly migrate your code. I think this was a brilliant move on John's part! :)

SolutionYogi
`var $ = document.getElementById;` does not work in Firefox or Google Chrome. You should use `function $(id) { return document.getElementById(id); }` instead. See http://stackoverflow.com/questions/1007340 for more information.
Grant Wagner
I didn't know that, thanks Grant. I can confirm that this used to work in IE6 as well as Firefox 2 because I frequently used this technique. I will update my main post.
SolutionYogi