+ Post New Thread
Results 1 to 2 of 2

Thread: Show code/advertisement/text only if page content do not contain certain phrasses

  1. #1
    Administrator
    Join Date
    Mar 2013
    Posts
    2,805

    Show code/advertisement/text only if page content do not contain certain phrasses

    WHich code i can use to show for example Adsense Ad only on pages that do not contains bad phrasses?

    ie: i have Adsense and i dont want to show ads on pages where in page content is "porn", "cigar"

    There is Javascript based code:

    <div id="notBarEnds">
    Here Add code to be shown if page is free from bad words
    </div>

    <script>
    if (/porn|sex|erot|cigar|drug/.test(window.location.href)) {
    document.getElementById('notBarEnds').style.displa y = 'none';
    }
    </script>
    Similar topic, if criteria is URL, not page CONTENT: Show code/advertisement/text only if page URL do not contain some phrasses

  2. #2
    Junior Member Maja22's Avatar
    Join Date
    Nov 2022
    Posts
    12


    Is this useful / helpfull? Yes | No
    Quote Originally Posted by Fli View Post
    WHich code i can use to show for example Adsense Ad only on pages that do not contains bad phrasses?

    ie: i have Adsense and i dont want to show ads on pages where in page content is "porn", "cigar"

    There is Javascript based code:



    Similar topic, if criteria is URL, not page CONTENT: https://pharma-hemp.com/product-category/cbd-drops/ Show code/advertisement/text only if page URL do not contain some phrasses[/URL]
    To display AdSense ads only on pages that do not zumba livestream contain specific "bad phrases" in their content, you can write JavaScript code that checks the page's content and hides the ad if any unwanted phrases are found.

    Here's how you can modify the approach you suggested to target page content (instead of just the URL):

    Targeting Page Content
    You can use JavaScript to scan the entire content of the webpage and then hide the ad container if it detects any of the banned phrases like "porn," "cigar," etc.

    <div id="notBarEnds">
    <!-- Here is where the AdSense code will go -->
    <ins class="adsbygoogle"
    style="display:block"
    data-ad-client="ca-pub-xxxxxxxxxx"
    data-ad-slot="xxxxxxxxxx"></ins>
    </div>

    <script>
    // Create a list of banned words
    const bannedWords = /porn|sex|erot|cigar|drug/gi;

    // Get the full page content as text
    const pageContent = document.body.innerText || document.body.textContent;

    // Check if any banned words exist in the page content
    if (bannedWords.test(pageContent)) {
    // If banned words are found, hide the ad
    document.getElementById('notBarEnds').style.displa y = 'none';
    } else {
    // Otherwise, show the ad
    (adsbygoogle = window.adsbygoogle || []).push({});
    }
    </script>

    In this code:

    The bannedWords regular expression holds the phrases you want to filter out (e.g., "porn," "sex," "cigar").
    pageContent grabs all the text from the page.
    The code checks if any of the banned phrases are present in the content using test(). If they are, the ad container with the ID notBarEnds is hidden by setting display: none.
    If the page is clean (no bad phrases), the AdSense ad will load normally.
    Targeting the URL (instead of content)
    If you want to hide ads based on the URL instead of the page content, the code is much simpler because you only need to check the window.location.href.

    <div id="notBarEnds">
    <!-- Here is where the AdSense code will go -->
    <ins class="adsbygoogle"
    style="display:block"
    data-ad-client="ca-pub-xxxxxxxxxx"
    data-ad-slot="xxxxxxxxxx"></ins>
    </div>

    <script>
    // Create a list of banned words in the URL
    const bannedWordsInURL = /porn|sex|erot|cigar|drug/gi;

    // Check if any banned words exist in the URL
    if (bannedWordsInURL.test(window.location.href)) {
    // If banned words are found in the URL, hide the ad
    document.getElementById('notBarEnds').style.displa y = 'none';
    } else {
    // Otherwise, show the ad
    (adsbygoogle = window.adsbygoogle || []).push({});
    }
    </script>
    In this case:

    The code checks the current page URL (window.location.href) for the banned phrases.
    If a banned phrase is found, the ad container is hidden.
    Otherwise, the AdSense ad is displayed normally.
    Important Considerations:
    Always test this thoroughly to make sure it's compatible with AdSense policies. Google has strict rules about how and when their ads can be displayed.
    Ensure that hiding ads based on content does not affect user experience or the functionality of the page.

+ Post New Thread

Similar Threads

  1. Javascript - click to hide/show text/content
    By Fli in forum Javascript Forum
    Replies: 2
    Last Post: 09-27-2020, 03:04 PM
  2. Replies: 0
    Last Post: 09-09-2015, 12:32 PM
  3. Replies: 0
    Last Post: 12-04-2014, 06:00 PM
  4. Replies: 0
    Last Post: 11-14-2014, 01:48 AM
  5. Replies: 0
    Last Post: 11-12-2014, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
 Protected by : ZB BLOCK  &  StopForumSpam