<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>断桥残雪部落格Image</title>
	<atom:link href="http://www.js8.in/tag/image/feed" rel="self" type="application/rss+xml" />
	<link>http://www.js8.in</link>
	<description>专注前端开发，分享JS、jQuery、PHP、CSS、WEB开发技巧</description>
	<lastBuildDate>Wed, 08 Sep 2010 09:58:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<ttl>30</ttl>
		<item>
		<title>IE中Image.onload失效的问题</title>
		<link>http://www.js8.in/501.html</link>
		<comments>http://www.js8.in/501.html#comments</comments>
		<pubDate>Tue, 09 Mar 2010 00:43:02 +0000</pubDate>
		<dc:creator>断桥残雪</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[网络技术]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.js8.in/?p=501</guid>
		<description><![CDATA[为了方便加载速度，我使用了new Iamge的对象方法来预加载图片，可是到了IE下进行调试总是不会触发onload事件，不管是IE8、IE7还是万恶的IE6，都不会触发onload事件，我很迷惑，后来在google上找到了解决的方法]]></description>
			<content:encoded><![CDATA[<p>最近在做wBox开发中，需要做一个图片的幻灯片效果，为了方便加载速度，我使用了<strong>new Iamge</strong>的对象方法来<strong>预加载图片</strong>，当图片onLoad就运行一个函数来计算图片的大小尺寸，进行<a title="jQuery easing动画效果扩展" href="http://www.js8.in/451.html" target="_blank">jQuery动画</a>，以及切换图片src，在firefox下开发完成后，可是到了IE下进行调试总是不会触发<strong>onload</strong>事件，不管是IE8、IE7还是万恶的IE6，都不会触发onload事件，我很迷惑，后来在google上找到了<strong>解决</strong>的方法：一下为引用部分</p>
<p>最初的代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #339933;">;</span>
img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;test.gif&quot;</span><span style="color: #339933;">;</span>
img.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span> img.<span style="color: #660066;">width</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这段代码看着没什么问题，但是为什么onload没有被IE调用呢？因为IE会缓存图片，第2次加载的图片，不是从服务器上传过来的，而是从缓冲区里加载的。是不是从缓冲区里加载的图片就不触发onload事件呢？我于是我测试了以下代码，成功了～</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #339933;">;</span>
img.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span> img.<span style="color: #660066;">width</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;test.gif&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>我把onload写到前面去，先告诉浏览器如何处理这张图片，再指定这张图片的源，这样就正常了。所以，不是IE没有触发onload事件，而是因为加载缓冲区的速度太快，以至于没有运行到img.onload的时候，onload事件已经触发了。这让我想到了Ajax，我们在写xmlhttp的时候，都是先指定onstatechange的回调函数，然后再send数据的，道理是一样的。
<div class="copyright"><span class="fav"><span class="icon"></span><span><strong>收藏本文到：</strong><a target="_blank" rel="nofollow" href="http://shuqian.qq.com/post?title=IE中Image.onload失效的问题&#038; uri=http://www.js8.in/501.html" title="收藏本文到QQ书签" target="_blank">QQ书签</a> | <a target="_blank" rel="nofollow" href="http://www.google.com/bookmarks/mark?op=add&#038;hl=zh_cn&#038;title=IE中Image.onload失效的问题&#038;bkmk=http://www.js8.in/501.html" title="收藏本文到Google书签" target="_blank">Google书签</a> | <a target="_blank" rel="nofollow" href="http://cang.baidu.com/do/add?it=IE中Image.onload失效的问题&#038;iu=http://www.js8.in/501.html" title="收藏本文到百度搜藏" target="_blank">百度搜藏</a> | <a target="_blank" rel="nofollow" href=" http://share.renren.com/share/buttonshare.do?link=http://www.js8.in/501.html&#038;title=IE中Image.onload失效的问题" title="收藏本文到人人网" target="_blank">人人网</a> | <a target="_blank" rel="nofollow" href="http://www.douban.com/recommend/?url=http://www.js8.in/501.html&#038;title=IE中Image.onload失效的问题" title="收藏本文到豆瓣" target="_blank">豆瓣</a> | <a target="_blank" rel="nofollow" href="http://www.kaixin001.com/repaste/share.php?rtitle=IE中Image.onload失效的问题&#038;rurl=http://www.js8.in/501.html" title="收藏本文到开心网" target="_blank">开心网</a> | <a target="_blank" rel="nofollow" href="http://myweb.cn.yahoo.com/popadd.html?title=IE中Image.onload失效的问题&#038;url=http://www.js8.in/501.html" title="收藏本文到Yahoo收藏" target="_blank">Yahoo收藏</a> | <a target="_blank" rel="nofollow" href="http://vivi.sina.com.cn/collect/icollect.php?title=IE中Image.onload失效的问题&#038;url=http://www.js8.in/501.html" title="收藏到新浪ViVi" target="_blank">新浪ViVi</a> | <a target="_blank" rel="nofollow" href="http://leshou.com/post?act=shou&#038;title=IE中Image.onload失效的问题&#038;url=http://www.js8.in/501.html" title="收藏本文到乐收" target="_blank">乐收</a></span></span><span><span class="icon"></span><span><strong>声明：</strong>文章未声明为原创文章，本文链接 <a rel="external nofollow" href="http://www.js8.in/501.html" title="IE中Image.onload失效的问题">http://www.js8.in/501.html</a>. 转载请注明转自<a rel="bookmark inlinks" href="http://www.js8.in" title="Theo Wong来自于青岛普加智能有限公司"> JS8.IN ™</a></span></span></div>
<h3>相关文章</h3>
<ul class="related_posts">
<li><a href="http://www.js8.in/454.html" title="jQuery 1.4的十五大新功能实例精讲" rel="bookmark inlinks">jQuery 1.4的十五大新功能实例精讲</a></li>
<li><a href="http://www.js8.in/529.html" title="通过javascript实现整站黑白效果" rel="bookmark inlinks">通过javascript实现整站黑白效果</a></li>
<li><a href="http://www.js8.in/331.html" title="14条JS代码编写时最佳规范" rel="bookmark inlinks">14条JS代码编写时最佳规范</a></li>
<li><a href="http://www.js8.in/620.html" title="关于wBox iFrame回调关闭问题的解决方案" rel="bookmark inlinks">关于wBox iFrame回调关闭问题的解决方案</a></li>
<li><a href="http://www.js8.in/377.html" title="UTF-8页面引用外部编码为GBK的JS文件编码的处理" rel="bookmark inlinks">UTF-8页面引用外部编码为GBK的JS文件编码的处理</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.js8.in/501.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
