Jesus 'n Jim

A mainly PC (some Mac) site w/Software, Computer Repair Info, How-To's on Using Computers
Technical Support 1-360-521-2060 (my business line cell)

blocking ads the hard way (but oh, so geekesque!)



this is my technique to block ads. it is based on the idea of using the hosts file to block ads from a Microsoft MVP, however, I discovered that that particular ad-blocking hosts file the MVP gave also blocked my ability to shop online in some places, and also made ( non-functional due to the ads being a dependency of the rest of the javascript code, and if a piece wasn't there, the whole page refused to work.

this is basically what I do to block ads. put an entry for the ad server in my hosts file (not the URL, but the domain, using ::1 for the IP address).

I use ff together with either firebug or it's built-in debugger's inspector to once it's identified, I copy the domain (either with my memory or with ctrl-c)

this may be useful for IT folk. many of the same servers are used for the same ads. I have a collection of about 100 ad servers I have blocked. I didn't supply a list since this can be a highly subjective thing. although for parents sakes (for sanity and safety of their kids), I may include a sample hosts file.

I have discovered along the way that many of the ad servers no longer use IPV4. they now use IPV6 - so instead of using for an IP address, you instead use ::1 in general. yes, it makes a difference. I was using and could not figure out why this one ad would not block.

I don't mind harmless ads. but I do mind ads advertising dates and everything downhill from there.

I suppose you could use ad-blocking software, but I have heard there are problems sometimes with using those. so I just do it the best way I know how. I have a skill for testing. and using a programmer's text editor like notepad++. and I know HTML fairly well (what I don't remember I can always look up). and I can use a debugger. (in this case, it deosn't require you to actually look at javascript code, just HTML and you are only looking for the domain part of a URL, so relax)

caveats - you should know this first before trying!

this is purely up to you. be careful how you do this, if you block the wrong domain, some of those ad servers also serve up js, and the main js code SOMETIMES is dependent on the ad code, and if it is, and the ad code didn't load because the domain points to your local machine, things like some e-commerce/shopping sites won't function completely or at all. so you have to test and be wary of what you do. if you use this, you are on your own.


well, maybe now it's time to tense up a bit... print this out. the basic idea here is to copy domains from <img> tags, <object> tags, <embed> tags, and optionally <a> tags and paste them into notepad++ with a ::1 and a space in front, each entry on its own line. got it? don't got it? steps 1-11 just configure your environment so you can get the job done. steps 12-26 are the actual task.

  1. install firefox or chrome and use as main browser (import your bookmarks form the other browsers and tell ff to continue the script repeatedly) - something that has a debugger with a tag/property inspector. I looked at
  2. install notepad++, make sure when you do you run it as administrator so it works right.
  3. [windows-logo-flag-key]-[pause/break], advanced, environment variables, System variables, pick Path, Edit..., hit the [End] key. type C:\Program Files (x86)\Notepad++; and hit [Enter]
  4. click OK, OK. this will make Notepad++ easily accessible from the cmd shell or from the start menu search box.
  5. right [agghh - win8 doesn't have the ability to right click the cmd shell and run as admin! no start menu! have to do it the hard way folks (but actually makes things easier in the long run)...] right click on the desktop and pick new, shortcut. put in c:\windows\system32.exe or %windir%\system32\cmd.exe for the path to the executable.
  6. for the working directory, put in c:\windows\system32\drivers\etc or %windir%\system32\drivers\etc
  7. click advanced, make sure run as administrator is checked
  8. rename the shortcut (general tab) to admin cmd shell.
  9. click OK. I don't fully remember corretly, but if you delete this shortcut, you may not be able to use a cmd shell again without a windows reinstall/repair/refresh and some programs may not install or uninstall right which use the cmd shell, because sometimes deleting the shortcut can delete the file (at least it's that way with folders - I don't like that, there should be a safe unlink). keep that admin shell around, it will come in handy lots of times. things run from it will not have to be run as administrator. for instance, you can do a memory speed test by doing winsat memformal from the admin cmd shell or run chkdsk c: /f /r to fix the filesystem and refresh a hard drive (/r shortens the life of ssd's and flash so don't use on those).
  10. right click the shortcut and pick pin to taskbar.
  11. click the admin cmd shell you just created on the taskbar
  12. type notepad++ hosts and hit [Enter]. you should already be in c:\windows\system32\drivers\etc where the hosts file is located. it is a protected file and is readonly except when accessed from an admin cmd shell.
  13. visit the page with the offensive ad
  14. pull up the debugger. most browsers it's under the tools[, advanced] menu, ctrl-shift-i to bring up the debugger, in chrome you have to additionally click the leftmost button on the dbeugger toolbar on the bottom to make the debugger a popup window.
  15. click on the inspector (repeatedly make sure it's selected until you have your target, because it can turn off, and you don't want to click the ad!). in chrome, it's the magnifying glass. in ff it's the box with a mouse pointer inside it.
  16. inspect the image or swf on a page using the inspector by hivering over it. you are looking for an <object>, <img>, <embed>, or surrounding <a> tag. you can examine that <a> tag's href= attribute to see if you want to also block where the ad leads to (where the ad links/jumps/goes to and where the ad image comes from can be 2 different things).
  17. when you have the offending ad in your sights, and you have a usable domain, click. this will select it and keep the code highlighted.
  18. select/highlight and copy the domain with ctrl-c - please NOTE: if the domain matches the main web site's domain, or if there is no domain, you must skip this ad since blocking the domain will block whole the web site (of course, if you DO want to block certain sites to protect your kids' browsing habits... also consider they offer an easy-to-setup proxy filtering service, and they offer a free and a pay filtering service that works at the router level if you need that).
  19. switch to notepad++. go to bottom of file (ctrl-[End]).
  20. hit [Enter], type ::1 followed by a space. then paste (ctrl-v) in the domain. basically the entry needs to be on a new line. it needs to look something like ::1
  21. switch back to browser. refresh page to get another ad (as many times as necessary, sometimes they only change every certain amount of time)
  22. repeat to step 13 as often as necessary.
  23. switch to notepad++ and hit ctrl-s to save.
  24. close notepad++
  25. close the admin cmd shell
  26. close the browser
  27. [windows-logo-flag-key]-R taskmgr [Enter] or ctrl-alt-delete to start the task manager.
  28. click Applications tab
  29. wait until the browser is not listed in the task manager. I have 60+ tabs, and this can take a minute to close.
  30. close the task manager.
  31. start up ff again. ads should be gone now. for some reason, this requires a restart. I think it cleans the browser cache.
  32. be sure to test by hitting F5 to refresh the page to change ads.
  33. if there is still a problem, correct the entry in the hosts file by repeating to step 12.
  34. for each extra machine in the house, repeat steps 35-36 one time for each computer:
  35. visit this page and download/view the hosts.txt file (this is not the same filename as the hosts file!) and do ctrl-a ctrl-c to select all and copy to clipboard.
  36. for the rest of the machines in the house, do steps 2-12. append the clipboard contents to what's already there by doing ctrl-[End] ctrl-v ctrl-s in notepad++ which will go to end of file, paste, and save. then close notepad++ and the admin cmd shell.

...and yes, this is automatic for me, it goes pretty fast when you know what you are doing! just takes work on the testing part, editing is easy, especially with multiple monitors. no more ugly or yucky ads! yay! PTL!

alternative uses

  • skip steps 34-36. the list can be converted to simply a domain "blocking" list for use in your router by either not putting in the ::1 or doing an alt-drag and selecting the column with the ::1's and spaces in it and hitting the [Delete] key.
  • include steps 34-36. the above list can have http:// prepended to each line so it can be used in ie or other browser site-blocking list.

how the hosts file works

  • entries are each on their own line and look like
  • you can comment out stuff using a hash mark (mesh, pound) # like this:
    (the # is actually a comment from the # to the end of the line).
  • hint: with notepad++, you can put in ::1 followed by a space for entire columns by alt-dragging to make a column cursor, and then just typing. this also works for cut/copy/paste.
  • TCP/IP looks here first when it needs to look up a domain. if it can't find the domain in the hosts file, it uses DNS (Domain Name Service) network protocol over the internet as a client. that's how this idea works. it finds your bad ad server in the hosts file, and doesn't go looking any further. it thinks the ad server is your machine, and since your machine isn't serving up the ads, HTTP basically fails to load that file.
  • c:\windows\system32\drivers\etc where the hosts file is located. it is a protected file and is readonly except when accessed from an admin cmd shell.
  • you can also use instead of ::1 if you want to make your own test & development web servers by using nonexistent domains like fred (not so an entry for that would look like fred this works over your local LAN since typically most home routers work with IPV4 mostly because of NAT. an IPV6 version of NAT is just an idea right now.


Download Now
hosts.txt - source and executable (10/14/2013, 3KB)