0

I am trying to remove and add div back to the DOm on window resize .

I have two google adds generated in my mobile-web page at different position . According to the google policy the mobile page can have only one add.

But I had to show the google adds at diffenret place according to different window size/mobile device etc etc . So i was just hiding one of them using Media QUeries .

But as the other div was also in the dom it is against the policy . So I used following script

<script>
$(document).on('pagecreate','#outerPage',function(e) {
   var windowWidth = $(this).width();
   if(windowWidth <300)
   {
       $('.addBigphone').remove();
   }
   else
   {
       $('.addSmallphone').remove();
   }
});
</script>

the html looks like this

<div id="bloque" class="addSmallphone">
    <?php // if($showadslast==t rue){?>
    <div class="google_add">
        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <!-- resultmobile -->
<ins class="adsbygoogle" style="display:inline-block;width:320px;height:90px" data-ad-client="ca-pub-8" data-ad-slot=""></ins>

        <script>
            (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
    </div>

<div id="bloque1" class="addBigphone">
        <?php // if($showadslast==t rue){?>
        <div class="google_add">
            <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
            <!-- resultmobile -->
    <ins class="adsbygoogle" style="display:inline-block;width:320px;height:90px" data-ad-client="ca-pub-8" data-ad-slot="04"></ins>

            <script>
                (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        </div>

Now it works only on page load .

Note I want to save the content of the div and add it back on different resize. How can I do that

Thanks in advance

1

2 Answers 2

2

I think this is what you want. You should use detach rather than remove when you plan to re-insert the element in the DOM.

var $bigPhoneAdd = $('.addBigphone'),
    $smallPhoneAdd = $('.addSmallphone');

$(window).resize(function(e) {
   var windowWidth = $(this).width();

   if (windowWidth < 300) {
       $bigPhoneAdd.detach();

       //You should append at desired insertion point...
       //Perhaps it could be made dynamic by storing $('.addBigphone').parent()
       //when the page loaded.
       $smallPhoneAdd.appendTo(?);
   }
   else {
       $smallPhoneAdd.detach();

       //You should append at desired insertion point...
       $bigPhoneAdd.appendTo(?);
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

yes I guess this is exactly what i wanted . And yes as the adds are generate next to an <li></li> tag so I think I should save the parent location to make it dynamic . Thanks I am going to try this code now .
@Vikram It might not work as is, but it should get you going ;) You can either save it's parent location, or prev/next sibling if it has to be inserted in between elements.
note: I analyzed (as of today, 2016) the google script, and there (no longer) is any document.write(), only stuff like document.createElement() and document.documentElement.appendChild()... so if problems occurs, they should habe different reasons.
1

Sounds like you should be using the resize event.

jquery http://api.jquery.com/resize/

You can detect the size of the window on resize, store any data you need, and take action as the window size changes.

4 Comments

@tinyethan I am already using the media queries for that . But what I want to do exactly is save the content of the div in a variable and when you resize the page I add the Div back to the DOm along with the content
How will this prevent 2 adds being in the DOM? CSS can't remove elements from the DOM.
@Vikram you should be able to do that using the jquery resize event... plalx you're right, I've edited my answer.
@tintyethan okay bro I am trying to do that :) thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.