我在iframe中遇到了麻煩。我試圖動態創建iframe,並根據用戶選擇將其src屬性分配給適當的文件。我使用內嵌JavaScript創建了一個簡單的html頁面,它重現了這個問題。IE iframe和unicode文本
<html>
<head>
<script type="text/javascript">
function createIFrame(){
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.id = "myframe";
}
function loadFile(fileName){
var frame = document.getElementById("myframe");
frame.src = fileName;
}
</script>
</head>
<body>
<input type="button" value="create iframe" onclick="createIFrame()" />
<input type="button" value="load ansi" onclick="loadFile('ansi.txt')" />
<input type="button" value="load unicode" onclick="loadFile('unic.txt')" />
<hr/>
<iframe id="workingFrame" src="unic.txt"></iframe>
</body>
</html>
ansi.txt - 以ANSI編碼保存的文本文件,unic.txt是unicode文本文件。
按下創建按鈕iframe被添加到DOM。如果按下「加載ansi」按鈕,文件內容將無任何問題加載到iframe中,但「加載unicode」按鈕會導致問題 - iframe保持空白。
但是,如果src屬性最初定義爲'workingFrame',則unicode文件加載正確。這個問題發生在IE 8中,IE 9建議下載文件而不是渲染它。我需要在iframe中呈現文件內容。
謝謝您的回覆。是的,我已經在幾臺PC上運行Windows 7的IIS 7下自己嘗試過了。問題是可重現的。我用Windows記事本創建了文本文件,改變了它的編碼保存選項。 – 2011-06-06 18:39:33
好吧,我得到它在ie9&8中重現錯誤,我錯過了用記事本編碼文件的部分。 – AsherMaximum 2011-06-06 21:05:44
儘管如此,在ie9中,即使在最初定義的iframe中也不會加載它。通過將文件擴展名更改爲.html,我確實可以在ie9和8中使用它。我猜這對你來說可能不是一個可行的選擇。 – AsherMaximum 2011-06-06 21:07:23