Skip to main content

A module filter.

Defer JavaScript

JavaScript

Defers script execution until the page has loaded.

Filter defer_javascript · Filter docs · Read the full guide

Both frames render identically — that's the goal. The win is in the bytes and requests below, not the look. They're served live by the mod_pagespeed module on demo-httpd-1.1.modpagespeed.com; the optimized frame applies only this filter. Right after a cache purge it may briefly match the original while the worker rewrites it — reload to see the result. mod_pagespeed applies this filter only for browsers it recognizes as supporting it, so the measurement requested the optimized page as a browser.

Measured impact

In this capture the HTML document grows from 1.0 KB to 1.8 KB, the page makes 6 HTTP requests instead of 5, and the total weight (HTML plus the files it loads) rises from 264.5 KB to 285.5 KB, 8% larger, so there's no byte or request reduction to chart. The change shows in the source diff below.

What changed in the source

The page's HTML, before and after this filter. Red lines are removed, green lines are added.

                
                  
… 1 unchanged line …
<head>
<title>defer_javascript example</title>
</head>
- <body>
+ <body><noscript><meta HTTP-EQUIV="refresh" content="0;url='https://demo-httpd-1.1.modpagespeed.com/mod_pagespeed_example/defer_javascript.html?PageSpeed=noscript'"/><style><!--table,div,span,font,p{display:none} --></style><div style="display:block">Please click <a href="https://demo-httpd-1.1.modpagespeed.com/mod_pagespeed_example/defer_javascript.html?PageSpeed=noscript">here</a> if you are not redirected within a few seconds.</div></noscript>
<!-- All Script tags will be deferred until page onload is triggered -->
<p>
If defer_javascript is enabled, '*'s and images will appear before script
text. If not the script text will be interspersed with '*'s and images.
</p>
- <script>
+ <script type="text/psajs" data-pagespeed-orig-index="0">
function sleep(ms) {
var sleep_now = Number(new Date());
while(sleep_now+ms>Number(new Date())) { var tmp = sleep_now; }
… 2 unchanged lines …
var state = 0;
</script>
<p>
- <script src="rewrite_javascript.js" type="text/javascript" id="ext1">
+ <script src="rewrite_javascript.js" data-pagespeed-orig-type="text/javascript" id="ext1" type="text/psajs" data-pagespeed-orig-index="1">
</script>
</p>
<p>*</p>
<img src="images/Puzzle.jpg"><br/>
<p>*</p>
- <script>
+ <script type="text/psajs" data-pagespeed-orig-index="2">
sleep(2000);
document.write('<p>Inline Script 1</p>');
</script>
<p>*</p>
<img src="images/BikeCrashIcn.png"><br/>
<p>*</p>
- <script>
+ <script type="text/psajs" data-pagespeed-orig-index="3">
sleep(2000);
document.write('<p>Inline Script 2</p>');
</script>
<p>*</p>
<img src="images/Cuppa.png"><br/>
<p>*</p>
- </body>
+ <script type="text/javascript" src="/pagespeed_static/js_defer.Do-RnQUcVf.js"></script></body>
</html>

Run this on your own site

This is one of 49 filters the mod_pagespeed module applies in place — self-hosted on Apache, nginx, and IIS. Install and run it: it optimizes right away and adds the optimized response headers shown above. Open source under the Apache License 2.0 — free in development and in production.

Search