Stop browsers accessing html image somehow?

hello,

i’ve got an html page which near the end of it has a particular image (a normal html <img src… one). is there any javascript way to stop browsers accessing that image from the server before it attempts to do so?

thanks

No - there isn’t even a way to prevent it using server side processing.

The best you can do is to set up an entry in the .htaccess file that prevents it being directly accessed from other sites so that someone must at least visit a page on your site (or set the headers so it looks like they are on a page of your site) before requesting direct access to the image.

what i’ll do is, once the server has ascertained js is being used by a particular user, next time the cookie image is requested by that user, supply it back to them with a header saying ‘cache this for this session’ rather than the usual ‘don’t cache this’. edit: oh no, that’s not going to work because the image is cookie’d; cookies stop caching. oh well, not to worry.

thanks.

I’m afraid that that’s not possible. It sounds like you’re wanting to apply a graceful-degradation technique, where instead we have now realised that progressive enhancement is the better way to go.

Start with coding at a base level without css or javascript, add on css for presentation, then use javascript to enhance the behaviour of the site.

Javascript can not stop images from being loaded after they already exist on the page.

So, a next-best solution is to compare requests for the image against requests for a javascript file that is also on that same page. The difference between them should tell you a lot.

> If you’re going to make an image publicly available to visitor via a web browser, you can not stop people taking copies of that image.

i know. i just want js people not to have to ask for this particular image. it’s part of a homemade analytics/data collection scheme. the image in question is a 1x1 image for cookie purposes (to allow the html web page to be cached but still get cookie id upon repeat visits to it), but for people who have js the cookie image isn’t required, supurflous, so want to turn it off as it were.

If you’re going to make an image publicly available to visitor via a web browser, you can not stop people taking copies of that image.

If you’re wanting to protect your work for some reason, you do have the option of watermarking it, or on using low resolution versions of the image.

To what purpose are you wanting to apply this protection? It may well be that suitable techniques already exist that we can advise you on.

> No - there isn’t even a way to prevent it using server side processing.

oh right, thanks.

there’d be no way of somehow falsly loading the image, maybe associating an image supplied by js (e.g. a 1x1 pixel image generated/embedded by js in the html’s head) with the url of the image, in the browser’s cache, therefore when the browser comes to load the image it uses the cached (not from the server but from js) one?

of course you can preload an image, just wondering if there’s a way of preloading (and associating with a particular url) but without actually making a network request? kind of falsly preloading.

probably not.

anyway, thanks for the info/reply.