How to Block Ads Instantly with a JavaScript Bookmarklet

4 min read
How to Block Ads Instantly with a JavaScript Bookmarklet


Ads are the internet’s necessary evil—until they’re not. While ad blockers like uBlock Origin or AdGuard are popular, they require installations, permissions, and updates. What if you could remove ads *instantly* without adding a single extension? Enter the **JavaScript Bookmarklet**, a tiny snippet of code that acts like a magic wand for ad-free browsing. In this guide, I’ll show you how it works, why it’s effective, and how to set it up in under 2 minutes.


Why Use a JavaScript Bookmarklet?

Ad-blocking extensions are powerful, but they come with trade-offs:  

  • They consume memory.  
  • They require access to your data.  
  • Websites often detect and block their usage.  


A JavaScript bookmarklet bypasses these issues. It’s lightweight, works on any browser (Chrome, Firefox, Edge), and leaves no trace. Think of it as a “one-click cleanup” tool for ads. 

 

How This Bookmarklet Works

This script uses smart targeting to identify and remove ad elements. Here’s the breakdown:  

1. Targets Ad Patterns:  

  • Removes elements with classes like `adsbygoogle`, `gpt-ad`, or `adthrive`.  
  • Blocks iframes loading ad scripts (e.g., Google Ads iframes).  
  • Detects GDPR cookie walls disguised as “consent” pop-ups.  


2.Ignores Critical Content:  

  • Skips elements inside `<article>` tags or containers marked `ad-layout` to avoid breaking site layouts.  


3. Works Instantly:  

  • No page reloads. Click the bookmarklet, and ads vanish mid-browse.  

Step-by-Step: How to Set It Up

1. Copy the Code:

Create a new bookmark in your browser and paste this code into the URL field

javascript:(function(e){function t(e){return["DIV","SPAN"].includes(e.tagName)}var a={elem(t){(function(t){for(let a of e.ignore?.selector??[])if(t.matches(a))return!0;for(let a of e.ignore?.func??[])if(a(t))return!0;return!1})(t)||(n.add([t,t.parentElement]),t.remove())},list(e){Array.from(e).forEach((e=>a.elem(e)))},cls(e){a.list(document.getElementsByClassName(e))},selector(e){a.list(document.querySelectorAll(e))},func({func:e,selector:t=null}){let n=null==t?document.getElementsByClassName("*"):document.querySelectorAll(t);for(let t of n)e(t)&&a.elem(t)}},n=new Set,o=new Set;for(let[t,n]of Object.entries(e))if("ignore"!=t)for(let e of n)a[t](e);for(let[e,r]of n)o.has(e)||(o.add(e),r.isConnected&&t(r)&&(r.hasChildNodes()||a.elem(r)))})({cls:["adsbygoogle","mod_ad_container","brn-ads-box","gpt-ad","ad-box","top-ads-container","adthrive-ad"],selector:[%27[aria-label="advertisement"]%27,%27[class*="-ad "],[class*="-ad-"],[class$="-ad"],[class^="ad-"],[class^="adthrive"]%27,%27:is(div,iframe)[id^="google_ads_iframe_"]%27,"#aipPrerollContainer","span[data-ez-ph-id] span[data-ez-ph-owner-id] span.ezoicwhat"],func:[{selector:'[class*="ad" i],[id*="ad" i]',func(e){for(const t of[e.id,...e.classList])if(/(?<!lo|re|he)(ad|Ad|AD)(vertisement)?s?([tT]hrive)?([cC]ontent)?([eE]ngine|[nN]gin)?([cC]ontainer)?s?($|[-_,\s])/.test(t))return!0}},{selector:"div#preroll",func(e){for(let t of e.children)if(t.matches("div#aipBranding"))return!0}},{selector:"html > iframe",func:e=>!(!e.sandbox.contains("allow-scripts")||!e.sandbox.contains("allow-same-origin")||2!=e.sandbox.length||!e.src.toLowerCase().includes("gdpr"))}],ignore:{selector:["body",".ad-layout","#game-holder.game-holder-with-ad",".no-interstitial-ads"],func:[e=>{let t=document.getElementsByTagName("article");for(let a of t)if(e.contains(a))return!0}]}});

2. Name the Bookmark:

Label it “Ad Blocker” or something memorable.  


3. Use It: 

Navigate to any site (e.g., a news blog or YouTube), click the bookmark, and watch ads disappear.  


### Pros and Cons 

✅ Pros:  
  • Zero installation—works on all browsers.  
  • No tracking or permissions required.  
  • Updates automatically (no manual tweaks).  

❌ Cons:  
  • May occasionally miss *very* new ad formats.  
  • Some sites might briefly “flicker” as ads reload.  


### FAQ  

Q: Is this legal?
A: Yes! You’re not modifying websites—just hiding elements locally.  

Q: Will it slow down my browser?  
A: No. It runs once per click and uses minimal resources.  

Q: Can I customize the ad-blocking rules? 
A: Absolutely! Modify the code’s `cls` or `selector` arrays to block new ad types.  



Conclusion

A JavaScript bookmarklet is the Swiss Army knife of ad-blocking—simple, efficient, and private. Whether you’re battling YouTube prerolls, invasive pop-ups, or cookie consent walls, this tool gives you back control over your browsing experience. Give it a try, and say goodbye to distractions!  

Bookmark this guide, grab the code above, and enjoy a cleaner web today. For more tech hacks, subscribe to our newsletter!