在IE下將一個form的target指向JS構造的iframe卻總是彈出新視窗
不得不說這個問題困擾了我很久,百度、google了一大把,想起了那句話:“有問題找百度,百度不行就谷歌,谷歌再找不到就自殺。”
中文搜尋了一大通還是找不到答案,改成英文的,很快找到問題所在,看來以後要多變換一下思維,不要在慣性思維的樹上吊死了。
原文如下:
Anyway, people found a relatively popular way to do this. The ideia is having a hidden iframe on the page, and use that iframe as a target of the form where you have the reference to the file to upload. So, when the form submits, the result page will go to the hidden iframe, and the user won’t see a full page refresh. You still have to take care of some details, like hiding the form and showing a nice progress bar with the classic “Hold on, we are uploading” message, and polling for the content of the iframe to check is the file is still in its way, or if it arrived safely.
One nice detail is where do you actually have the hidden iframe. You could just put the iframe on the HTML code of your page, but IMO that’s ugly. The iframe is an artifact that is needed just to serve as a black hole for the form submittion result page. It doesn’t make sense to put it in your HTML code, as it hasn’t anything to do with the content. Also, it’s error prone: you may inadvertently delete it, causing the file upload to missbehave. Or you may need to change the iframe code and having it on HTML will force you to update on all the pages where you use it (and of course, you will forget one).
So, I believe the most elegant solution (if you can use the word “elegance” in the context of this major hack) is to generate the iframe using javascript. You may do this on the page load, when you create the form, whatever. Just do it before the user submits the file! :) Well, it’s easy, right? Something like this does the trick:
var objBody = document.getElementsByTagName("body").item(0);
var iframe = document.createElement('iframe');
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
objBody.insertBefore(iframe, objBody.firstChild);
That’s cool, right? Just run this and the iframe will be created. Submit the form, the result goes in the hidden iframe, everything works, we are done, let’s go home. Well… all true until you actually test it on Internet Explorer…
If you test this on IE, the result will be a new window being created when you submit, which is clearly not what you want. Well, I had a hard time finding the problem, so here goes: if you create the iframe using JavaScript, IE wont set it’s name. Yes, the “iframe.name = ‘fileUploaderEmptyHole’;” line will simply be ignored. It does nothing. So, as you don’t have any frame called ‘fileUploaderEmptyHole’, when you submit the form, it will create a new window named ‘fileUploaderEmptyHole’ and display the result on it.
The solution it to hack this even further. Specifically:
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
Yeah! Now you’re thinking “WTF?”. Yes, yes, it’s true. This actually works on IE, with the expected (?) results. Well, you still have to support the other browsers, but you are lucky, as this will throw an exception on all the non-IE browsers. So, it’s just a matter of catching it, and running the decent version of the code:
var iframe;
try {
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
} catch (ex) {
iframe = document.createElement('iframe');
}
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
This is why I love the Web. Or maybe not.
關鍵是這句話:
If you test this on IE, the result will be a new window being created when you submit, which is clearly not what you want. Well, I had a hard time finding the problem, so here goes: if you create the iframe using JavaScript, IE wont set it’s name. Yes, the “iframe.name = ‘fileUploaderEmptyHole’;” line will simply be ignored. It does nothing. So, as you don’t have any frame called ‘fileUploaderEmptyHole’, when you submit the form, it will create a new window named ‘fileUploaderEmptyHole’ and display the result on it.
問題所在:在IE下,通過JS建立一個iframe object,對其name屬性的賦值操作將被忽略。
解決方式:在IE和其他browser下可以通過以下方式建立iframe
var iframe;
try { // for I.E.
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
} catch (ex) { //for other browsers, an exception will be thrown
iframe = document.createElement('iframe');
}
英文很容易讀,剩下的就自己動手解決吧。
相關文章
- 在框架頁中彈出新視窗提供列印功能框架
- jQuery在iframe裡取得父視窗的某個元素的值jQuery
- JS彈出視窗視窗的位置和大小JS
- 解決JS彈出新視窗被瀏覽器阻止的解決方案JS瀏覽器
- JS 中 this 在各個場景下的指向JS
- js實現在彈出視窗中重新整理主視窗JS
- iframe裡面的頁面呼叫父視窗,左右視窗js函式的方法JS函式
- 一個 vuejs 的 form 表單提交結構VueJSORM
- 一行js彈窗程式碼就能設計漂亮的彈窗廣告JS
- js漸變彈出視窗和關閉視窗效果JS
- 關於myeclipse 總是彈出ThreadPoolExecutor 視窗的解決辦法Eclipsethread
- 關於父視窗獲取跨域iframe子視窗中的元素跨域
- javascript在iframe子元素中獲取父視窗元素JavaScript
- XP系統一開機便彈出新硬體嚮導視窗怎麼辦?
- js為showModalDialog()彈出視窗的頁面傳值JS
- 彈出視窗
- JS 中的函式 this 指向總結JS函式
- fastadmin 彈出視窗的功能AST
- Win10系統中ie瀏覽器總是自動彈出指令碼偵錯程式視窗如何解決Win10瀏覽器指令碼
- js實現的點選彈出確認視窗程式碼JS
- cgo 如何將C返回的結構體指標指向下一個地址?Go結構體指標
- 在不把視窗設定成當前視窗的條件下,對視窗進行操作。
- 同一個語句在plsql的sql視窗可以執行命令視窗不能執行SQL
- 在Watir中整合AutoIt處理JavaScript彈出視窗的方法JavaScript
- 點選按鈕在iframe子視窗載入指定頁面
- 一個基於mpvue的toast彈窗元件mptoastVueAST元件
- mptosat,一個基於mpvue的toast彈窗元件VueAST元件
- 如何總是在新視窗中開啟資料夾?
- Prism 彈出視窗
- JS this的指向JS
- js的this指向JS
- js頁面彈窗JS
- jQuery iframe子視窗獲取父視窗元素簡單介紹jQuery
- 廣告彈窗/小視窗程式碼
- js實現window.open()彈出視窗和父視窗之間相互操作JS
- 在RFT中關閉所有IE瀏覽器視窗的3種方法瀏覽器
- 在列印視窗,列印檢視View的子檢視結構圖View
- FireFox和IE下使用Date來構造新Date物件的BUGFirefox物件