Monday, 15 May 2017

How to Load Disqus Comments on Click

The Disqus widget is loaded asynchronously meaning it downloads the JavaScript in parallel and would not therefore impact the load time of your web pages. That said, the widget still adds lot of weight to your pages as the Disqus files will download on the user’s computer even if they aren’t interested in participating in the discussion. The other issue with auto-loading Disqus is that it makes your pages lengthy especially when viewed on mobile devices.



LOAD DISQUS ON DEMAND WITH JAVASCRIPT

As an alternative, you can configure Disqus on your website to load on-demand and not automatically. When someone clicks a button the widget will be dynamically added to your web page and not otherwise. This lazy-loading technique can be implemented in pure JavaScript without jQuery.

Step 1: Go to your web page template that has Disqus and replace the #disqus_thread <div> with the following snippet:

<div id="disqus_thread">
  <a href="#" onclick="disqus();return false;">Show Comments</a> 
</div>
Step 2: Next place the Disqus code before the close <head> tag of your web page. You’ll have to replace the disqus variables – like disqus_shortname, disqus_url, etc. – with your own parameters.

<script type="text/javascript">

// Replace way2trick with your disqus shortname
var disqus_shortname = "way2trick";

// Put the permalink of your web page / blog post
var disqus_url = "http://example.com/blog-post";

// Put the permalink of your web page / blog post
var disqus_identifier = "http://example.com/blog-post"; 

var disqus_loaded = false;

// This is the function that will load Disqus comments on demand
function disqus() {

  if (!disqus_loaded)  {
    
    // This is to ensure that Disqus widget is loaded only once
    disqus_loaded = true;
    
    var e = document.createElement("script");
    e.type = "text/javascript";
    e.async = true;
    e.src = "//" + disqus_shortname + ".disqus.com/embed.js";
    (document.getElementsByTagName("head")[0] ||
     document.getElementsByTagName("body")[0])
    .appendChild(e);
  }
} 

</script>


The page will have a “Show Comments” button and the comments are only loaded when the button is clicked.

Some websites have auto-loading enabled for Disqus but the widget is loaded when the reader has scrolled to the bottom of the  article. This can again be done in JavaScript. We can use the onscroll method to check whenever the page is scrolled and if the user is near the bottom, the script will load the Disqus widget.

Place this snippet near the closing </body> tag of your page.

<script type="text/javascript">
  window.onscroll = function(e) {
    if ((window.innerHeight + window.scrollY) 
        >= document.body.offsetHeight) 
    {
        if (!disqus_loaded) disqus(); 
    }
};
</script>

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Labels

404 AdBlock Add-on Airtel GPRS Trick Airtel SMS Trick Alexa Amazon Amazon Kindle Amazon Prime Android Android 8 Android Oreo antivirus Apple Apple Mac ASCII Audacity Audio Authotkey Backup Balance Transfer in Vodafone Battery Bing Blogger Blogging Bookmarklet Browser Camera Chromebook clock Cloud colors command lines Computer Computer Tricks configuration Contact Creative Commons Credit Card CSS devolop DIY Doodle DOS Download Dropbox E-Mail eBook Email Email Attachment Embed Encryption English Error Evernote Eyes Facebook Facebook Tricks Feedburner Flipkart Font Foursquare Free Internet Free sms trick in Vodafone G Mail Gadget Game Getty Images GIF Gists Github Google Google AdSense Google Analytics Google Apps Google Chrome Google Contacts Google Currents Google DNS Google Docs Google Drive Google Earth Google Font Google Forms Google Images Google Map Google Photos Google Play Store Google Plus Google Print Google Reader Google Script Google Sheets Google Spreadsheet Google Translate GPRS Setting GPS Hacking Health App HelloFax Hindi Hoodie HTML Icons idea Image Editing Images IMEI Indian Railways Infographics Instagram Internet Internet Explorer Internet Tricks iOS iPad iPhone IRCTC iTunes iTV JavaScript JioCinema JioTV Junglee Kindle Language Translation Laptop Laptop. TV Life Time FREE GPRS Life-Style Link Linkedln Linux logo Make Money Online Microdoft Powerpoint Microdoft Word Microsoft Office Microsoft Outlook Mobile Mosaic Music Name Networking nexus Notepad OCR Online Shopping Open DNS OS Outlook Password PDF Petya Phillips Hue Lights Photogtraphy Pixel Play Station Podcasts Pokemon Pokemon Go Polls Print Productivity Proxy Server Pushbullet QR Code Ransomware Reddit Reliance Hack GPRS Reliance Jio RGB Ringtone Router RSS Safe Mode Samsung Galaxy S Scrabble Screen Capture Screen Sharing Screencast Secrets Security Send free sms from PC SEO Sierra Skype Slideshare SMBv1 SMS Snapchat Snapdeal Social Media Solution Sound Device Speech Recognition Sql Steam Sync Synology NAS Tata Docomo GPRS trick Teleprompter Torrent Trick Tricks TV Twitter UltraISO Unicode Unknown Extension Unlimited 2GB Unlimited 3GB Unlimited GPRS USB USB Security Key Video Editing virtual desktop Virus attack VLC Vodafone 110% working trick for GPRS Vodafone 3g Vodafone GPRS VPN wallpapers WannaCry Web Design Web Domain Website Wget Whatsapp WiFi Wikipedia Windows Windows 10 Windows 10 S Windows KN Windows Tricks windows updates Winows N Wolfarm Alpha WordPress XBox YouTube Zip
Twitter Delicious Facebook Digg Stumbleupon Favorites More