Whenever I modify the Google ad code (shown in red below) in order to remove the extra spacing on my blog's search page, the ad doesn't show on other pages of my blog where it should. The search page is just for testing the extra-spacing on the side-bar. How can I solve the extra-spacing issue on the side bar by modifying the ad code (where the problem is) without making the ad disappear?
Code:
<div id="demo2"></div>
 
<script>
;(function()
  {
    var pageHeight = 0;

    function findHighestNode(nodesList)
      {
        for (var i = nodesList.length - 1; i >= 0; i--)
          {
            if (nodesList[i].scrollHeight && nodesList[i].clientHeight)
              {
                var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
                pageHeight = Math.max(elHeight, pageHeight);
              }
            if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
          }
      }

    findHighestNode(document.documentElement.childNodes);

    // The entire page height is found
  
    if (pageHeight >= 665)
      {
        document.getElementById("demo2").innerHTML = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- advert1_on_side-column -->
<ins class="adsbygoogle"
     style="display:inline-block;width:304px;height:254px"
     data-ad-client="ca-pub-2444862488094022"
     data-ad-slot="1987958613"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</script>