简体   繁体   中英

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.

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 .

With Firefox you can Greasemonkey to do exactly this with all the power of Javascript.

If you don't mind using jquery

on body tag

<body onload="remove()">

javascript(jquery)

function remove()
{
    $('img').each(function() {
        if(this.src.search('somehostname') != -1)
            $(this).remove();
    });
}

Not sure if they fired image request or not,but they won't come in your sight for sure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM