<?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>Silentash-默尘 &#187; frameborder</title>
	<atom:link href="http://blog.silentash.com/tag/frameborder/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.silentash.com</link>
	<description>尘埃默默，风雨洗礼</description>
	<lastBuildDate>Tue, 24 Aug 2010 16:31:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Iframe Tips ABC</title>
		<link>http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw</link>
		<comments>http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw#comments</comments>
		<pubDate>Wed, 09 Dec 2009 00:52:28 +0000</pubDate>
		<dc:creator>nunumick</dc:creator>
				<category><![CDATA[JS/Ajax/AS/Flex]]></category>
		<category><![CDATA[frameborder]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[name]]></category>
		<category><![CDATA[onload]]></category>
		<category><![CDATA[target]]></category>

		<guid isPermaLink="false">http://mick/project/Php/silentash/?p=152</guid>
		<description><![CDATA[通常我们用 js 脚本创建 iframe 时，会这样写：<br/><p class="code"><code>&#160;&#160;&#160;&#160;var&#160;iframe&#160;=&#160;document.createElement('iframe');<br/></code></p>之后我们可能会定义 id、name、border 等属性，这些看似简单，其实 IE 与非 IE 浏览器之间、IE 和 IE 高版本之间的迥异，使得 iframe 的相关兼容性操作非常地有文章...<br/><br/>
]]></description>
			<content:encoded><![CDATA[<p>通常我们用 js 脚本创建 iframe 时，会这样写：</p>
<p class="code"><code> var iframe = document.createElement('iframe'); </code></p>
<p>之后我们可能会定义 id、name、border 等属性，这些看似简单，其实 IE 与非 IE 浏览器之间、IE 和 IE 高版本之间的迥异，使得 iframe 的相关兼容性操作非常地有文章。</p>
<h3>牛A：frameborder</h3>
<p>现象：使用 (iframe.frameBorder = 数值) 或(iframe.setAttribute(&#8216;frameborder&#8217;,数值)) 在 IE 浏览器下无效<br />
原因：IE 浏览器区分属性名称大小写<br />
解决方法：iframe.setAttribute(&#8216;frameBorder&#8217;,'0&#8242;) Or iframe.setAttribute(&#8216;frameborder&#8217;,'0&#8242;,0)，兼容所有浏览器。</p>
<p>注：经测试，IE8已经修复此问题</p>
<h3>牛B：动态将Form target到iframe</h3>
<p> 背景：假设现在我们要让一个 Form 表单结果提交到一个 HTML 结构中已存在的 iframe，会这样做：</p>
<p class="code"><code> &lt;form id="form" name="form" target="相应iframe的name:iframeNB" method="post" &gt;<br />
  &lt;/form&gt;<br />
  &lt;iframe name="iframeNB" &gt;&lt;/iframe&gt; </code></p>
<p>OK，什么问题也没有，再假设我们需要提交到脚本动态生成的 iframe 中，会这样做：</p>
<p class="code"><code> &lt;form id="form" name="form" target="iframeNB" method="post" &gt;<br />
  &lt;/form&gt;<br />
  &lt;script&gt;<br />
  var iframe = document.createElement('iframe');<br />
  iframe.name = 'iframeNB';<br />
  ...<br />
  someParent.appendChild(iframe);<br />
  &lt;/script&gt; </code></p>
<p>去 IE 浏览器里试试，你会发现 Form 会在新窗口显示提交结果，Why?<br />
原因：<br />
我为此尝试了很久，结果是IE此前版本不能通过(iframe.name=)这种方式给 iframe 设置 name 值，也就是说生成的 iframe 是没有 name 值的，但却可以 alert 出来，这很诡异；当然，这也并不是没有解决办法。<br />
解决方法，为此我们得为 IE 单独写一行代码：</p>
<pre class="chili"><code class="js">  /*only for ie */
    var iframe = document.createElement(&#039;&lt;iframe name=&quot;iframeNB&quot;&gt;&#039;);
</code></pre>
<p>看到这行代码，我们笑了，这是天大的杯具（喜剧？）～～不管IE有多么搓的问题，他总会有自己一套解决之……<br />
而且这行代码会在其他非 IE 浏览器抛出异常，所以我们可以利用这点来做最终版：</p>
<pre class="chili"><code class="js">    var iframe;
    try{
        iframe = document.createElement(&#039;&lt;iframe name=&quot;iframeNB&quot;&gt;&#039;);
    }catch(e){
        iframe = document.createElement(&#039;iframe&#039;);
    }
    iframe.name = &#039;iframeNB&#039;;
    ...
    someParent.appendChild(iframe);
    ...
</code></pre>
<p>[2009-12-9]补充：最佳实践 &#8211; YUI 是如何 creat iframe 的</p>
<pre class="chili"><code class="js">    /**
    * @description Creates an iframe to be used for form file uploads.  It is remove from the
    * document upon completion of the upload transaction.
    * @method createFrame
    * @private
    * @static
    * @param {string} optional qualified path of iframe resource for SSL in IE.
    * @return {void}
    */
    function _createFrame(secureUri){
    // IE does not allow the setting of id and name attributes as object
    // properties via createElement().  A different iframe creation
    // pattern is required for IE.
    var frameId = &#039;yuiIO&#039; + this._transaction_id,io;
    if(YAHOO.env.ua.ie){
        io = document.createElement(&#039;&lt;iframe id=&quot;&#039; + frameId + &#039;&quot;name=&quot;&#039; + frameId + &#039;&quot; /&gt;&#039;);
        // IE will throw a security exception in an SSL environment if the
        // iframe source is undefined.
        if(typeof secureUri == &#039;boolean&#039;){
            io.src = &#039;javascript:false&#039;;
        }
    }else{
        io = document.createElement(&#039;iframe&#039;);
        io.id = frameId;
        io.name = frameId;
    }
    io.style.position = &#039;absolute&#039;;
    io.style.top = &#039;-1000px&#039;;
    io.style.left = &#039;-1000px&#039;;
    document.body.appendChild(io);
    YAHOO.log(&#039;File upload iframe created. Id is:&#039; + frameId, &#039;info&#039;, &#039;Connection&#039;);
    }
</code></pre>
<p>这里需要额外注意到的一点是：</p>
<p class="code"><code> // IE will throw a security exception in an SSL environment if the<br />
  // iframe source is undefined.<br />
  if(typeof secureUri == 'boolean'){<br />
      io.src = 'javascript:false';<br />
  } </code></p>
<p>姑且算是牛D吧 =.=!</p>
<h3>牛C：iframe.onload</h3>
<p>关于 onload 这点大家可以参考<a href="http://www.planabc.net" target="_blank">怿飞师父</a>的文章：<a href="http://www.planabc.net/2009/09/22/iframe_onload/" target="_blank">判断 iframe 是否加载完成的完美方法</a>，在此纯引用一次代码： </p>
<pre class="chili"><code class="js">var iframe = document.createElement(&quot;iframe&quot;);
iframe.src = &quot;http://www.planabc.net&quot;;
if (iframe.attachEvent) {
    iframe.attachEvent(&quot;onload&quot;, function () {
        alert(&quot;Local iframe is now loaded.&quot;);
    });
} else {
    iframe.onload = function () {
        alert(&quot;Local iframe is now loaded.&quot;);
    };
}
document.body.appendChild(iframe);
</code></pre>
<p><strong>需要注意到的是：</strong></p>
<ul>
<li>IE8也不支持iframe.onload</li>
<li>Opera两者均可，所以使用此方法会绑定前者</li>
<li>即使我们不预设iframe.src = some urls，也会默认执行一次onload事件，可以通过分析 src 规避。</li>
</ul>
<p>附测试文件：<br />
1.<a href="http://www.silentash.com.previewdns.com/upload/iframe_ie_ugly.html" target="_blank">iframe_ie_ugly.html</a><br />
2.<a href="http://www.silentash.com.previewdns.com/upload/iframe_fixed.html" target="_blank">iframe_fixed.html</a><br />
以上，我认为 ABC 中最牛的就是 B 了，这也是我标题的亮点=.=! 斯密达们认为呢？ </p>
<hr /><h2>Comments</h2><ul><li><a href="http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw#comment-129">2009年12月9日</a>, Tcer writes: B的确很有意思, 我第一次看见这样用是在看YUI源码的时候.
</li><li><a href="http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw#comment-130">2009年12月9日</a>, Tcer writes: 第一次看见B这样的用法, 是在看YUI的时候, 当时就被雷了, 扛伤斯密达!<blockquote><div class="quote quote3"><div class="quote-title">nunumick 于 2009-12-9 9:30:20 回复</div>没错，YUI的setform方法就在upload时用到了这个技巧，的确是个很好的例子，稍候补上。</div></blockquote>
</li><li><a href="http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw#comment-131">2009年12月13日</a>, <a href='http://www.9sh.net' rel='external nofollow' class='url'>仁心博客</a> writes: 学习下了
</li><li><a href="http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw#comment-136">2009年12月26日</a>, 嘻嘻嘻 writes: // IE will throw a security exception in an SSL environment if the
// iframe source is undefined.
if(typeof secureUri == 'boolean'){
io.src = 'javascript:false';
}

这个还是没看明白
能解析一下么</li></ul><hr /><small>Copyright &copy; 2008-2010<br /> This feed is for personal, non-commercial use only. <br /> </small>]]></content:encoded>
			<wfw:commentRss>http://blog.silentash.com/2009/12/iframe-tips-abc/?fuckgfw/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
