Simply mimic the 'defer' attribute for inline scripts across all browsers (jQuery helper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://bit.ly/qDefer | |
$(function(){$('script[type="text/javascript/defer"]').each(function(){$(this).clone().attr('type','').insertAfter(this)})}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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