javascripthtmldisqus

Ignore the scripts for a specific page


I have been trying to make a blog in publii. And let's assume that I made a home page, a Contact Us page and an article page. So I have been trying to implement comments using Disqus. So for that, I have put the code below as custom HTML after every post.

<div id="disqus_thread"></div>
<script>
    (function() { // DON'T EDIT BELOW THIS LINE
    var d = document, s = d.createElement('script');
    s.src = 'https://websitenamehere.com/embed.js';
    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

So by default this script will run on every post I make, ie it won't run on the home page but will come on the contact us page and article page. I don't need it on the contact us page so is there any HTML code to ignore this footer on the contact us page only?


Solution

  • You could prevent this function to run if the path of the current page meets some criterias. For example, you could edit your script to that :

    
    if(
      !window.location.pathname.startsWith('/contact')
      &&
      !window.location.pathname.startsWith('/about-us')
    ){
    
    (function() { // DON'T EDIT BELOW THIS LINE
        var d = document, s = d.createElement('script');
        s.src = 'https://websitenamehere.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
    
    }
    
    

    Of course you should change /contact and /about-us to the urls used in your website !