6

Is it possible to detect if an image has been loaded via:

<img src="image.jpg"/>

Versus in the address bar or linked to directly via:

<a href="image.jpg">Image</a>

Thanks.

anonymous-one
  • 14,454
  • 18
  • 60
  • 84

2 Answers2

5

No, in general it is not.

The browser will issue the exact same request for both.

When the browser parses the HTML (first thing it gets), each additional resource (JavaScript, CSS, image and other linked files) will be requested separately. Browsers do not add information about where they got the reference from - so it is not possible to tell from the request whether it was directly on the address bar or from a reference on an HTML page.

You could query the logs to see if the request is all on its own or if a request to the HTML page (and possibly other resources) have been done around the same time. This is not fool proof (think about the multiple levels of caching we have on the web).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Normally the `Accept` request header will differ. See [here](https://stackoverflow.com/a/29416515/4830897). But i really wouldn't trust this to always work. F.e. will it break the download image function in ff. – SleepProgger Jun 22 '17 at 21:14
2

It is possible.
Just check the referrers!

<img src="image.jpg"/>

The referrer will be the page BEFORE the image

<a href="image.jpg">Image</a>

The referrer will be the page where the image is embedded

Otherwise, just add a GET param.

<img src="image.jpg?src=img"/>

<a href="image.jpg?src=a">Image</a>

On the following page with JS or in the apache logs you eval the GET param without any side effects or pitfalls for the user.

Anish Gupta
  • 2,218
  • 2
  • 23
  • 37
gekkstah
  • 76
  • 1
  • 1
    Referrer is far from perfect - it can be spoofed and not all browsers behave the same. – Oded Sep 01 '12 at 14:36