0

I'm trying to make a website mobile-friendly. One of the steps is to hide the right column, which has ads and non-essential data. In the CSS, I have something like this:

@media only screen {
    @media handheld or (max-width: 768px) {
        #column_right {display:none}
    }
}

The problem is that the right column contains javascript to display ads, which will still be executed. This is bad for 2 reasons: 1. It slows down the loading of the page. 2. It is against the ad networks' TOS (because the view is still registered, even though the visitor doesn't see an ad)

How would I prevent the code in the hidden column from being executed?

Edited to add: Here is one of the javascript code blocks that I need to disable:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- removed ad block name -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="removed"
     data-ad-slot="removed"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
3
  • How is the ad code implemented? Through a <script> block or manually through your own JS? Commented Nov 1, 2013 at 4:18
  • Through a <script> block. I.e. exactly the way I got the code from the ad networks. Commented Nov 1, 2013 at 4:54
  • I just added the code of one such block (it's from Google AdSense) Commented Nov 1, 2013 at 4:57

3 Answers 3

0

If you're using jQuery you could check if the element is visible. http://api.jquery.com/visible-selector/

0

I'd suggest jQuery with a $(window).width() check. You can call this on $(document).ready() as well as listen for $(window).resize().

If you're listening for the resize, you of course want to do something like:

var MAX_WINDOW_WIDTH = 768;

checkWindowSize();

$(window).resize(checkWindowSize);

function checkWindowSize(){
    if ($(window).width() < MAX_WINDOW_WIDTH && $('#column_right').size() > 0){
        $('#column_right .ad-block').remove();
    }
    if ($(window).width() > MAX_WINDOW_WIDTH && $('#column_right').size() == 0){
        $('#column_right').append('[some HTML ad code here]');
    }
});
0

No you can not prevent to load a code if you write it on page. Because Javascript/Jquery code is render when DOM in ready state.

You can prevent execution of code-block, by checking the visibility property.

Like that:

var theCSSprop = getComputedStyle( document.querySelector("#column_right") ).visibility

if(theCSSprop != "hidden"){
  // write your code which you want to prevent 
}

If you want to improve page performance than move JavaScript to the bottom of the page instead of putting them in the head/top.

Read This

See live example

1
  • I'm lost... what is "#div1"? Why do we do var elem1 = document.getElementById("column_right"); when we don't use it anywhere after that? Commented Nov 1, 2013 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.