May 2017
Intermediate to advanced
448 pages
10h 10m
English
Occasionally, we don't want to retrieve all the JavaScript we will need when the page is first loaded. We might not know what scripts will be necessary until some user interaction occurs. We could introduce the <script> tags on the fly when they are needed, but a more elegant way to inject additional code is to have jQuery load the .js file directly.
Pulling in a script is about as simple as loading an HTML fragment. In this case, we use the $.getScript() function, which--like its siblings--accepts a URL locating the script file:
$(() => { $('#letter-c a') .click((e) => { e.preventDefault(); $.getScript('c.js'); });});
In our last example, we needed to process the result data so that we could do something ...
Read now
Unlock full access