今天看见有人因img的src为空导致session丢失,以前一直没注意这个,所以晚上特地写了一段代码验证:
Default.aspx后台代码:
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 void WriteLog() { 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(); } } } }
代码很简单,页面每次打开,都会在log/log.txt中追加一行记录,内容为“当前页的绝对网址+当前时间”前端测试代码:
<%@ 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" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" /> </body> </html>
F5运行,发现log.txt确实有二行记录(在IE,FF,Chrome下都是同样的结果)
如果把<img alt="test" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" /> 改成二个连续的img,即:
<img alt="test" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" />
<img alt="test" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" /> 运行后,页面仍被执行2次
继续测试,把<img alt="test" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" /> 改为 <img alt="sss" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" onerror="this.src='http://www.baidu.com/img/baidu_logo.gif'"/>,即图片加载错误时自动加载百度的logo,保证最终src肯定有值猜一下运行结果?结果与不加onerror完全相同,页面仍然执行二次!(有点意外吧?哈...)
go on! 这回干脆把class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original=""都去掉,即改成<img alt="test" />,再次竞猜结果?居然正常了,即仅执行了一次!(呵呵,看来多一事不如少一事)
最后再折腾一次,把src故意写成一个错误的地址,比如<img alt="test" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://abc.asdf.werdafsaf.sadfasdfas.xxx.gif" />结果正常,仅执行了一次!
结论:img标签的class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original=""时,会引起浏览器再次访问"./"即当前目录,如果该目录下有default.aspx,index.aspx等IIS的默认文档,则默认文档会执行2次 .
共同学习,写下你的评论
评论加载中...
作者其他优质文章