An IMG tag is an empty trap

Source: Internet
Author: User

There is a huge risk that the IMG tag src in the page be set to "", whether it is written in HTML

or write in JS

var img = new Image (); IMG.SRC = "";

The appearance of such a label once will result in more requests being made to your server. The src= "" of the IMG tag will cause the browser to access the "./", which is the current directory, and the default document will execute 2 times if there is a default document for IIS such as default.aspx,index.aspx.

      • In IE, this will request the directory where the current page is located. The presence of this empty SRC tag in causes a request to be re-requested://

      • In Safari and Chrome, the current page itself is requested.

      • In the previous version of Firefox 3.5, there was the same problem as Safari, but this bug was fixed in 3.5.

      • In opera, no additional requests are made.

In a Web site with a low level of access, it doesn't matter if one of these requests (or even your web browsing seems to double), but in a tens, or even higher, website, this can lead to a significant increase in the cost of your server and bandwidth. Another pitfall is that re-requesting a page may cause some of the user's information to be inadvertently modified, such as cookies, or Ajax operations.

You would never write such a code?

I do not think so, many times this happens inadvertently, such as the following PHP code:

Your original plan is to read this SRC address from the server side, but for some reason the address is not set, or the code bug causes the read to fail, an empty src tag appears.

Will the empty src in the other tabs cause such a problem?

The good news is that only the image tag has this problem in IE. The bad news is that in Chrome, Safari, and Firefox, <script src= "" > and <link href= "> will cause a new request to appear.

How to solve this problem?

Can start from two aspects, one is to try to avoid this bad programming way, do not appear empty src tags. In addition, you can start from the server side and do not return anything to the client when this meaningless request is found.

<?PHP//Works for IE if using path URLs and not file URLs//get the referrer    $referrer=isset($_server[' Http_referer ']) ?$_server[' Http_referer ']: '; //Current URL (assuming HTTP and default port)    $url= "http://".$_server[' Http_host '].$_server[' Request_uri ']; //Make sure they ' re not the the same    if($referrer==$url){         Exit; } ?>

Original URL:

Empty image Src can destroy your site

An IMG tag is an empty trap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.