From 47320ef271184a22ebdab55f233c2a17a87f0f04 Mon Sep 17 00:00:00 2001 From: jhaapatalo Date: Mon, 16 May 2016 09:22:20 +0300 Subject: [PATCH] Fix javascript error when Disqus not included on the page. --- Duk.EPiServer.Disqus/Models/CodeBuilder.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Duk.EPiServer.Disqus/Models/CodeBuilder.cs b/Duk.EPiServer.Disqus/Models/CodeBuilder.cs index 120e837..f4140c1 100644 --- a/Duk.EPiServer.Disqus/Models/CodeBuilder.cs +++ b/Duk.EPiServer.Disqus/Models/CodeBuilder.cs @@ -18,9 +18,13 @@ public class CodeBuilder : ICodeBuilder const string LoaderScriptTemplate = @"${Parameters} (function() { - 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); + if($('#disqus_thread').length) { + 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); + } })();"; private const string ThreadTemplate = "
"; @@ -41,7 +45,7 @@ public string CreateLoaderScript(IConfiguration configuration, IContext context) AddParameter(context.Url, UrlParameterTemplate, ref parameters); AddParameter(context.Title, TitleParameterTemplate, ref parameters); AddParameter(context.CategoryId, CategoryIdParameterTemplate, ref parameters); - return LoaderScriptTemplate.Replace("${Parameters}", parameters.ToString()); + return LoaderScriptTemplate.Replace("${Parameters}", parameters.ToString()); } ///