Skip to content

Instantly share code, notes, and snippets.

@rafaelpatro
Forked from RonnyO/jquery.qDefer.min.js
Created October 23, 2017 00:25
Show Gist options
  • Save rafaelpatro/f92edfe0fa3b87e9b92378654a0a2f3d to your computer and use it in GitHub Desktop.
Save rafaelpatro/f92edfe0fa3b87e9b92378654a0a2f3d 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