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 (http://internetlifeforum.com/html-css-forum/2599-show-code-advertisement-text-only-if-page-url-do-not-contain-some-phrasses/)
Maja22
10-13-2024, 08:23 AM
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 (https://zumbaliciouscrew.com/en/online-zumba/) 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.
Powered by vBulletin® Version 4.2.5 Alpha 2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.