adblock no ad image

How To Remove Ads From Some WebPages

Updated Sunday, May 19, 2024, 7 PM

In your website, you might want to remove ads in some pages. To make this possible , for instance,You can use AdSense page exclusion feature to disable ads in some specific pages.

But there's also another way with a few lines of javascript code, and this article provides you an example code for how to remove ads in specific pages of your website

One way to disable ads in specific pages is by dynamically loading the ad code only on the pages where you want the ads to appear.

Here's how to remove ads in some pages

  1. Wrap your ad code (e.g., Google AdSense code) in a JavaScript function.
  2. Call this function only on the pages where you want the ads to appear.

For example, suppose you have the following AdSense code in your website's header:


<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-XXXXXXXXXXXXXXXX",
          enable_page_level_ads: true
     });

You can modify this code like this:


<script>
    function loadAds() {
        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-XXXXXXXXXXXXXXXX",
            enable_page_level_ads: true
        });
    }
</script>

Then, on the specific pages where you want to disable ads, you can remove the call to the `loadAds()` function. This prevents the ad code from executing on those pages.

By dynamically loading the ad code only on the pages where you want ads to appear, you effectively prevent the ad network from executing on specific pages of your website.

Comments

Add comment






No comments yet