The src = "of the IMG Tag causes page_load to be executed multiple times.

Source: Internet
Author: User

Today, I saw someone in the garden lose session because the SRC of IMG is empty. For more information, see http://www.cnblogs.com/kyneblog/archive/2009/06/11/1500999.html.

I didn't pay attention to this before, so I wrote a special article in the evening.CodeVerification:

Default. aspx background code:

Using System;
Using System. Web;
Using System. IO;

Namespace SRC
{
Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Writelog ();
}

Public Static VoidWritelog ()
{
String_ Logfile=Httpcontext. Current. server. mappath ("Log/log.txt");

Using (Streamwriter SW =   New Streamwriter (_ logfile, True ))
{
String S =   String . Format ( " {0} {1} " , Httpcontext. Current. Request. url. absoluteuri. tostring (), datetime. Now. tostring ());
Sw. writeline (s );
Sw. Close ();
}
}
}
} The code is very simple. Each time the page is opened, a line of record will be appended to log/log.txt with the content of "absolute website of the current page + current time" front-end test code: <% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Default. aspx. CS " Inherits = " SRC. _ default "   %>
<! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en "   " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< HTML xmlns = " Http://www.w3.org/1999/xhtml "   >
< Head runat = " Server " >
< Title > </ Title >
</ Head >
< Body >
< IMG alt = " Test " SRC = ""   />
</ Body >
</ Html > F5runtime, it is found that log.txt does have two rows of records (both in IE, ff, and chrome). If you change to two consecutive IMG, that is:

after running, the page is still tested twice, change to . That is, when an image loading error occurs, Baidu's logo is automatically loaded. Do you have a value to guess the running result of SRC? The result is exactly the same as that without onerror. The page is still executed twice! (Something unexpected, right? Ha...) Go on! In this case, we simply remove src = "" and change it to ? It is normal, that is, it is only executed once! (Ha ha, it seems that more than one thing less than one thing) Finally tossing again, the SRC deliberately written a wrong address, such as the result is normal, only once! Conclusion: When the src = "" of the IMG label is used, the browser may access it again ". /"indicates the current directory. If the directory contains default. aspx, index. if the default document of IIS, such as aspx, is executed twice by default.

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: info-contact@alibabacloud.com 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.