jQuery in Action
166
CHAPTER 6
jQuery utility functions
<script type="text/javascript">
var $ = 'Hi!';
jQuery(function(){
alert('$ = '+ $);
});
</script>
</head>
<body>
</body>
</html>
In this document, we import jQuery, which (as we know) defines the global names
jQuery
and its alias
$
. We then redefine the global
$
name to a string value
b
,
overriding the jQuery definition. We replace
$
with a simple string value for sim-
plicity within this example, but it could be redefined by including another library
such as Prototype.
such as Prototype.
We then define the ready handler
c
whose only action is to display an alert
showing the value of
$
.
When we load this page, we see the alert displayed, as shown in figure 6.3.
Note that, within the ready handler, the global value of
Note that, within the ready handler, the global value of
$
is in scope and has the
expected value resulting from our string assignment. How disappointing if we
only wanted to use the jQuery definition of
only wanted to use the jQuery definition of
$
within the handler.
Now let's make one change to this example document. Listing 6.3 shows only
the portion of the document that has been modified; the minimal change is high-
lighted in bold.
lighted in bold.
Overrides $ name
with custom value
with custom value
b
Declares the
ready handler
ready handler
c
Figure 6.3 The
$
says, "Hi!" as its redefinition takes effect within the ready handler.