Created
March 22, 2011 23:21
-
-
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
This file contains hidden or 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
<?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