Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created March 22, 2011 23:21
Show Gist options
  • Save franz-josef-kaiser/882311 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/882311 to your computer and use it in GitHub Desktop.
load jQuery in WordPress only when comments are inside the viewport
<?php
add_action ( 'wp_head', 'add_jquery_when_comments_in_viewport', 11 );
function add_jquery_when_comments_in_viewport()
{
?>
<script type="text/javascript">
/* <![CDATA[ */
function jquery_for_comments_onLoad() {
var element = document.getElementById( "comments" ),
current = document.body.clientHeight+document.body.scrollTop,
target = element.offsetTop;
if ( current >= target ) {
$.defer( "jquery-1.8.1.min.js", {
bare: true
} ).done( console.log( "jquery-1.1.1.min.js loaded" ) );
window.onscroll = null;
}
}
window.onscroll = jquery_for_comments_onLoad;
/* ]]> */
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment