Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how src attribute of img tag is executed?

Tags:

html

image

src

Consider the following code :

 <img src="http://website.com/Page/1"/>

"1" is a parameter to insert in some table in the "Page" page in website.com. Will the visitor to this page (that contains the code above) cause an execute of the page and insert 1 to the table ? If the page contains some Javascript code. Will it be executes if we visit the page that contains the code above ?

like image 337
Younes Ch Avatar asked Feb 19 '13 09:02

Younes Ch


People also ask

How does IMG src work?

The src attribute contains a URL pointing to the image you want to embed in the page. As with the href attribute for <a> elements, the src attribute can be a relative URL or an absolute URL. Without a src attribute, an img element has no image to load.

What is src attribute in IMG tag?

The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed.

What does the src attribute do?

Definition and Usage The src attribute specifies the location (URL) of the external resource.

How does IMG tag work in HTML?

The HTML <img> tag is used to embed an image in a web page. Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding space for the referenced image. The <img> tag is empty, it contains attributes only, and does not have a closing tag.


2 Answers

A browser, if configured to load and display images, will first check whether it has the document matching URL in its own cache as fresh (by some caching criteria). If not, it will send, via HTTP, a GET request to mysite.com for the resource /Page/1. What happens then is up to the server. It may just pick up an image file from its resources and send it to the browser, or it may generate an image an send it, or it might (instead of or in addition to such things) store or update something in its database, or just a counter in a file, or whatever it has been programmed to do.

If the resource sent by the server is image data, the browser will try to display it. If it happens to be e.g. an HTML document, it will be discarded, and the browser will display the value of the alt attribute instead, or an icon of a broken image, or both.

like image 95
Jukka K. Korpela Avatar answered Oct 11 '22 23:10

Jukka K. Korpela


When the browser finds this img tag in a visible area (so it should not be hidden with display: none for example), it executes the image as a http request. That's how statistic tracking works, too.

So as it's a regular http request it will execute the server-side code for that URL, which should in return deliver an image (be it just a blank 1x1 gif), so the browser does not report an error.

But keep in mind the browser might cache the image if you visit this page the second time. So either append a random string or timestamp at the end (e.g. http://website.com/Page/1?23423412341) or tell the browser with htaccess to not cache it.

like image 41
acme Avatar answered Oct 12 '22 00:10

acme