Search code examples
javascriptdisquslocation-href

Can I use location.href to generate the url for a variable?


I'm adding disqus to my website and I was wondering if there's a way to use the page's url to fill in the variable for disqus_url

I know that window.location.href will find the url, I just don't know how to use that inside of a function.

This is what I've tried:

<script type="text/javascript">
    (function() {
        var disqus_url = window.location.href;
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>

Solution

  • Yes, you can. However, window.location.href is the default, so you really don't need to provide it.

    Note that this will most likely treat http://example.com/helloworld.html and http://example.com/helloworld.html?123 as two different threads.

    See https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables#disqus_url