jQuery in Action
180
CHAPTER 6
jQuery utility functions
Although it's evident that this utility function can be useful in many scenarios
where one object must be extended with properties from another object (or set of
objects), we'll see a concrete and common use of this feature when learning how
to define utility functions of our own in the next chapter.
where one object must be extended with properties from another object (or set of
objects), we'll see a concrete and common use of this feature when learning how
to define utility functions of our own in the next chapter.
But before we get to that, let's wrap up our investigation of the utility functions
with one that we can use to dynamically load new script into our pages.
6.4 Dynamically loading scripts
Most of the time--perhaps, almost always--we'll load the external scripts our
page needs from script files when the page loads via
page needs from script files when the page loads via
<script>
tags in the
<head>
of the page. But every now and again, we might want to load some script after the
fact under script control.
fact under script control.
We might do this because we don't know if the script will be needed until after
some specific user activity has taken place but don't want to include the script
unless absolutely needed, or perhaps, we might need to use some information not
available at load time to make a conditional choice between various scripts.
unless absolutely needed, or perhaps, we might need to use some information not
available at load time to make a conditional choice between various scripts.
Regardless of why we might want to dynamically load new script into the page,
jQuery provides the
$.getScript()
utility function to make it easy.
Figure 6.5 Using
$.extend
to merge object results in all source properties being
copied into the
target
object.