Search code examples
javascriptimageblock

How can i block images from certain site with javascript?


I think this can be done with js..

Let's say i want to block loading images from example.com or even with a single image path?

I prefer the first variant. can i do this?

Example:

if a image is displayed in html with http://example.com/filepath, don't allow to display images from that domain or even the file path.


Solution

  • Probably not. You can add JavaScript at the bottom of the page or using the onLoad event but at that time, the browser might have already started the requests to load the images (it can start with that as soon as it reads the image URL).

    There simply is guarantee that your JavaScript executes before the highly-optimized URL loading of the browser kicks in.

    You also can't load the other page in an iframe thanks to Same Origin Policy.

    Instead, you should configure a proxy like Privoxy or use something like AdBlock.