Skip to content

Instantly share code, notes, and snippets.

@RonnyO
Created April 15, 2012 11:24
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save RonnyO/2391995 to your computer and use it in GitHub Desktop.
Save RonnyO/2391995 to your computer and use it in GitHub Desktop.
Simply mimic the 'defer' attribute for inline scripts across all browsers (jQuery helper)
// http://bit.ly/qDefer
$(function(){$('script[type="text/javascript/defer"]').each(function(){$(this).clone().attr('type','').insertAfter(this)})});
<!-- Somewhere along your page, you have some javascript which you want to defer. Just set its type to text/javascript/defer -->
<script type="text/javascript/defer">
$('body').css('background', 'green');
</script>
<!-- maybe even an external file -->
<script type="text/javascript/defer" src="http://cornify.com/js/cornify.js"></script>
<!-- then near the bottom, you load libraries & dependencies like you should -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<!-- Assuming one of them is jQuery, just add this thingy and voila! Your scripts will run on `ready`. Note: Execution order isn't guaranteed for external files -->
<script>$(function(){$('script[type="text/javascript/defer"]').each(function(){$(this).clone().attr('type','').insertAfter(this)})});</script>
<!-- Unminified, if you're interested:
<script>
$(function(){
$('script[type="text/javascript/defer"]').each(function(){
$(this).clone().attr('type', '').insertAfter(this);
});
});
</script>
-->
@shinsenter
Copy link

Defer/async script tags are not good enough

There is a common knowledge that you should use <script src=".." async defer> (or set script.async = true before assigning src, when you do it from JS) and/or put your scripts at the very bottom of the page, so that as much as possible of the page gets loaded and rendered to the user, as fast as possible.

defer.js is written in plain JavaScript, making lazy-loading other contents more fast and performant. You can defer any javascript files as well as inline script blocks efficiently.

Defer loading of JavaScript

Defer loading of JavaScript

All you need to do now is just wrap all your code in defer() function.

<script type="text/javascript">
    // This will defer showing an alert message after 2 seconds
    defer(function() {
        alert("This message is shown after 2 seconds after the 'load' event.");
    }, 2000);

     // Append a HTML content to the <body> tag using jQuery
     deferscript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js', 'jquery', 500, function () {
         $('body').html('<p>Your awesome content</p>');
     });
</script>

Or defer loading and execution of external scripts with deferscript() function.

<script type="text/javascript">
    // Alternative way to lazy load Google Tag Manager script
    deferscript('//www.googletagmanager.com/gtag/js?id=UA-34520609-2', 'google-tag', 500, function() {
        // Run extra code right after the script has been loaded
        (window.dataLayer = window.dataLayer || []).push('config', 'UA-34520609-2');
    });
</script>

If your page is just an HTML page enhanced with some JavaScript, then you're good with just <script async>. It takes time for browser to parse and execute those scripts, and each UI change may reflow your layout, make your load speed more slow, no one likes staring at a blank white page; users are impatient and will leave quickly.

In various cases, using async or defer does not deliver faster page speed than defer.js does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment