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!
webstuck5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
if(document.getElementById('ad_footer_2')!=null){
     document.getElementById('ad_footer_2_position').appendChild(document.getElementById('ad_footer_2'));
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
webstuck5Author Commented:
That looks great! Do you think referencing the missing DIV could be causing the web page to hang?
0
GaryCommented:
Js in itself wouldn't hang a page but it could break other js.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.