Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Forked from RonnyO/jquery.qDefer.min.js
Last active January 9, 2016 23:26
Show Gist options
  • Save bryanwillis/eabbffa4d63f7c5cde83 to your computer and use it in GitHub Desktop.
Save bryanwillis/eabbffa4d63f7c5cde83 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>
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment