Skip to content

Instantly share code, notes, and snippets.

@prinsss
Created February 9, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prinsss/e31ae00ddbadf1b0237c56a36e7ee6b1 to your computer and use it in GitHub Desktop.
Save prinsss/e31ae00ddbadf1b0237c56a36e7ee6b1 to your computer and use it in GitHub Desktop.
Originally created by @osvaldasvalutis, callback support by @printempw.
$.disqusLoader('.disqus', {
scriptUrl: '//blessing-studio.disqus.com/embed.js',
laziness: 0,
preLoadCallback: function () { $('.disqus-notice').show() },
disqusConfig: function () {
this.callbacks.onReady = [function () {
$('.disqus-notice').hide()
}];
}
});
/*
disqusLoader.js v1.0
A JavaScript plugin for lazy-loading Disqus comments widget.
-
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/
;( function( $, window, document, undefined )
{
'use strict';
var $win = $( window ),
throttle = function(a,b){var c,d;return function(){var e=this,f=arguments,g=+new Date;c&&g<c+a?(clearTimeout(d),d=setTimeout(function(){c=g,b.apply(e,f)},a)):(c=g,b.apply(e,f))}},
throttleTO = false,
laziness = false,
disqusConfig = false,
scriptUrl = false,
preLoadCallback = false,
postLoadcallback = false,
scriptStatus = 'unloaded',
$instance = $(),
init = function()
{
if( !$instance.length || $instance.data( 'disqusLoaderStatus' ) == 'loaded' )
return true;
var winST = $win.scrollTop();
// if the element is too far below || too far above
if( $instance.offset().top - winST > $win.height() * laziness || winST - $instance.offset().top - $instance.outerHeight() - ( $win.height() * laziness ) > 0 )
return true;
$( '#disqus_thread' ).removeAttr( 'id' );
$instance.attr( 'id', 'disqus_thread' ).data( 'disqusLoaderStatus', 'loaded' );
if( scriptStatus == 'loaded' )
{
DISQUS.reset({ reload: true, config: disqusConfig });
}
else // unloaded | loading
{
window.disqus_config = disqusConfig;
if( scriptStatus == 'unloaded' )
{
scriptStatus = 'loading';
if (typeof(preLoadCallback) == 'function')
preLoadCallback();
$.ajax(
{
url: scriptUrl,
async: true,
cache: true,
dataType: 'script',
success: function()
{
scriptStatus = 'loaded';
if (typeof(postLoadcallback) == 'function')
postLoadcallback();
}
});
}
}
};
$win.on( 'scroll resize', throttle(throttleTO, init));
$.disqusLoader = function( element, options )
{
options = $.extend({},
{
laziness: 1,
throttle: 250,
scriptUrl: false,
disqusConfig: false,
preLoadCallback: false,
postLoadcallback: false,
}, options );
laziness = options.laziness + 1;
throttleTO = options.throttle;
preLoadCallback = options.preLoadCallback;
postLoadcallback = options.postLoadcallback;
disqusConfig = options.disqusConfig;
scriptUrl = scriptUrl === false ? options.scriptUrl : scriptUrl; // set it only once
$instance = ( typeof element == 'string' ? $(element) : element ).eq( 0 );
$instance.data( 'disqusLoaderStatus', 'unloaded' );
init();
};
})( jQuery, window, document );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment