Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a script that loads jQuery when an element comes into the viewport, as discovered by Javascript (without a library), and it looks like this:

viewport.js:

var height = 0;
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    height = window.innerHeight;
} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    height = document.documentElement.clientHeight;
} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    height = document.body.clientHeight;
}

function gingah_comments_onLoad() {
    var element = document.getElementById("comments");
    var current = height+document.body.scrollTop;
    var target = element.offsetTop;
    console.log(current);
    console.log(target);
    if (current >= target) {
        if (typeof jQuery == 'undefined') {
            $.defer("http://localhost/wp/wp-includes/js/jquery/jquery.js", {
            }).done(console.log('loaded jQuery'));
            window.onscroll = null;
        }
    }
}
window.onscroll = gingah_comments_onLoad;

And the html used:

<head>
<script src="DeferJS.min.js" type="text/javascript"></script>
<script src="viewport.js" type="text/javascript"></script>
</head>
<body>
<div style="height: 1000px; border: 1px solid #ff0000;">Top</div>
<div class="comment_box" style="height: 1000px; border: 1px solid #ff0000;">
    <div id="comments">Comment1</div>
</div>
</body>

The DeferJS.min.js refers to a minified version of the following: https://github.com/BorisMoore/DeferJS/raw/master/DeferJS.js

However, when I try to implement this in Wordpress (3.1), having used wp_enqueue_script for both viewport.js and DeferJS.min.js, the scrollTop appears to not work at all. When the HTML and JS above is combined, it will log the values to the console, making it very clear when #comments came into the viewport.

But when replicating the process in Wordpress, the current var does not count upwards when scrolling down, but stays static. And, according to Firebug, viewport.js, DeferJS.min.js and l10n.js (Wordpress 3.1 localization) are the only scripts loaded. Also, Quirksmode lists the scrollTop method as available in practically all browsers.

What could be the cause of this malfunction in document.body.scrollTop?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It would seem that something in Wordpress causes a virtual revert to a standards that require cross-browser compatibility, making the same code not work inside and outside Wordpress. However, with some code to handle browser-differences (and still avoiding using libraries), the following is what viewport.js looks like now:

function height() {
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        return window.innerHeight;
    } else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientHeight;
    } else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        return document.body.clientHeight;
    }
}
function distance() {
    if(typeof pageYOffset!= 'undefined'){
        //most browsers
        return pageYOffset;
    } else {
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

function gingah_comments_onLoad() {
    var element = document.getElementById("comments");
    var current = height()+distance();
    var target = element.offsetTop;
    console.log(current);
    console.log(target);
    if (current >= target) {
        if (typeof jQuery == 'undefined') {
            $.defer("http://localhost/wp/wp-includes/js/jquery/jquery.js", {
            }).done(console.log('loaded jQuery'));
            window.onscroll = null;
        }
    }
}
window.onscroll = gingah_comments_onLoad;

Which then loads jQuery from the standard Wordpress-path when the #comments-element comes into view.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.