How jQuery protects jQuery and $ rewriting
This is not (but see below). If you load jQuery and then load something else that writes something else to these characters, they will no longer be associated with jQuery. Example:
HTML:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>
JavaScript:
window.onload = function() { // Outputs false, because Prototype has overwritten it display("Does $ === jQuery? " + ($ === jQuery)); // Outputs true, because Prototype has overwritten it display("Does $('foo').id === 'foo'? " + ($('foo').id === 'foo')); function display(msg) { var p = document.createElement('p'); p.innerHTML = msg; document.body.appendChild(p); } };
Live copy
But it looks like they keep them. How?
Answer closures . It has a local link inside its scope function, which is not tied to a global link ( window property), so internally it doesn’t care if you replace these characters. Of course, you care if you overwrite both $ and jQuery , because if you have no way to call jQuery. :-) But if you only overwrite $ , this is not a problem, just use jQuery or if you don't like typing this ( and let him look at him, this is inconvenient), do the following:
(function($) {
... which obscures the $ character locally inside an anonymous function (which is defined and called at the same time). jQuery makes this especially easy if you use the ready event:
jQuery(function($) {
source share