The Best of Both Worlds
Even if all the factors point to inlining, it still feels inefficient to add all that JavaScript and CSS to the page and not take advantage of the browser's cache. Two techniques are described here that allow you to gain the benefits of inlining, as well as caching external files.
Post-Onload Download
Some home pages, like Yahoo! home page and My Yahoo!, typically have only one page view per session. However, that's not the case for all home pages. Yahoo! Mail is a good example of a home page that often leads to secondary page views (pages that are accessed after the initial page, such as those for viewing or composing email messages).
For home pages that are the first of many page views, we want to
inline the JavaScript and CSS for the home page, but leverage external
files for all secondary page views. This is accomplished by
dynamically downloading the external components in the home page after
it has completely loaded (via the onload
event). This places the external
files in the browser's cache in anticipation of the user continuing on
to other pages.
- Post-Onload Download
The post-onload download JavaScript code associates the doOnload
function with the document's
onload
event. After a one-second
delay (to make sure the page is completely rendered), the appropriate
JavaScript and CSS files are downloaded. This is done by creating the
appropriate DOM elements (script
and link
, respectively) and assigning the specific ...
Get High Performance Web Sites now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.