Link to home
Start Free TrialLog in
Avatar of webstuck5
webstuck5

asked on

Missing DIV Tag Causes Web Page to Hang

I am using JavaScript to split test the layout of ads on my web page. Here is part of my web page code:

<script>
      var ads_to_show = "default";
      var random_number = Math.random();

      if (random_number < .5)
      {
        ads_to_show = 1;
      }
      else
      {
        ads_to_show = 2;
      }
    </script>

     <script>
        ad_client = "ca-pub-3412735816916817";
        ad1_display_code = ad2_display_code = ad3_display_code = '<scr' + 'ipt ' + 'src="http://pagead2.googlesyndication.com/pagead/show_ads.js">' + '</scr' + 'ipt>';

        if (ads_to_show == 2)
        {
          ad3_position = "ad_footer_2";
          /* RomanceStuck.com-Footer #2-Content-970x90-Text/Image-Google default-TEST 2 */
          ad3_slot = "4670775136";
          ad3_width = 970;
          ad3_height = 90;
        }
      </script>

      <script>
        google_ad_client = ad_client;
        google_ad_slot = ad3_slot;
        google_ad_width = ad3_width;
        google_ad_height = ad3_height;
        document.write('<div id="' + ad3_position + '" style="width=' + ad3_width + 'px; height=' + ad3_height + 'px;">');
          document.write(ad3_display_code);
        document.write('</div>');
      </script>

The DIV tag with id of "ad_footer_2" only loads if ads_to_show == 2. So, when ads_to_show == 1, that DIV tag doesn't exist but later on in my web page code I still have:

<div id="ad_footer_2_position">
        </div>
        <script>
document.getElementById('ad_footer_2_position').appendChild(document.getElementById('ad_footer_2'));
        </script>

I believe this appendchild code referencing a div that doesn't exist is causing the web page to hang when loading. Is this the cause of the web page hanging and if so, how can I rewrite my code to deal with it.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webstuck5
webstuck5

ASKER

That looks great! Do you think referencing the missing DIV could be causing the web page to hang?
Js in itself wouldn't hang a page but it could break other js.