1.什麼是iframe?
iframe閉合標籤,會創建包含另個文檔的內聯框架,針對不支持iframe的瀏覽器,可以將提示文字放在iframe之間。
2.iframe的作用
(1)通過iframe實現文件(附件)的下載
<a href="#" onclick="download()">download</a>
<iframe id="ifile" style="display:none"></iframe>
function download(){
var dom=document.getElementById('ifile');
dom.src="http:xxxx.com";
}
通過隱藏iframe,當點擊事件觸發的時候,動態設置src就能實現文件下載功能。
(2)通過iframe實現圖片上傳或者說表單的提交
<iframe id="upload_target" name="upload_target" src="upload.php" style="width:0;heigth:0;overflow:hidden;border:0;position: absolute; left:-500px;"></iframe>
<form enctype="multipart/form-data" action="upload.php" method="post" target="upload_target">
<input type="file" name="userfile" class="file" value="" />
<input type="submit" name="uploadimg" value="上傳" onClick="upload()"/>
</form>
這樣,點擊事件會觸發upload(),並且觸發表單提交,並且提交的同時不會刷新頁面。
3.iframe有什麼缺點
(1)iframe會阻塞主頁的onload事件
(2)搜索頁面無法解讀這種頁面(不利於SEO)
(3)iframe和主頁面會共享連接池,而瀏覽器對於相同區域有限制,所以會影響性能。