2013-04-18 22 views
1

下面的代碼顯示了問題。它創建了一個簡單的iframe。IE - 整個頁面在iframe src設置爲壞URL時重定向

如果url設置爲http:[4斜線] test.com而不是http:[雙斜槓] test.com,除了IE瀏覽器,iframe將顯示錯誤。在IE上父頁面重定向到錯誤頁面。

我在其他人的網站中使用了一個小部件的iframe,這可能會導致一個錯誤情況下的很多損害。我的問題是如何避免,所以如果SRC網址不好,錯誤將保留在iframe中,並且不會導致整個頁面重定向。

要檢查您可以將代碼複製到jsfiddle.net並運行。

popup = window.document.createElement('div'); 
popup_html="<div style='width:300px;height:300px; ' >"; 
popup_html+=" <iframe src='http:////www.test.com' width='300' height='300' frameborder='0' ></iframe>"; 
popup_html+=" </div>"; 
popup.innerHTML=popup_html; 
//cbar_popup.style.visibility='visible'; 
window.document.body.appendChild(popup); 
+0

在鉻中工作正常,似乎無法模仿你的問題。 – 2013-04-18 07:19:09

+0

問題是關於IE – Nir 2013-04-18 07:49:15

回答

1

你可以嘗試這樣的事情來清潔URL,如果URL是有效的只嘗試彈出的東西:

var url = 'http:////stackoverflow.com/questions/16076720/ie-entire-page-redirects-if-iframe-src-is-set-with-bad-url'; 
var result = /^(?:(?:http[s]?:\/{2,4})?(?:www\.)?)?([\w-_]+)\.([\w\.]+)([\/\?\w-_\=\"]*)/.exec(url); 

if(result){ 
    url = 'http://'+result[1]+'.'+result[2]+result[3]; 
    console.log(url); 
    // do the popup stuff here with cleaned up url 
} 
0

爲了防止你可以試試這個重定向。

popup = window.document.createElement('div'); 
popup_html="<div style='width:300px;height:300px; ' >"; 
popup_html+=" <iframe id='myframe' src='' width='300' height='300' frameborder='0' ></iframe>"; 
popup_html+=" </div>"; 
popup.innerHTML=popup_html; 
//cbar_popup.style.visibility='visible'; 
window.document.body.appendChild(popup); 
document.getElementById('myframe').src="http:////www.test.com"; 

我不知道它發生的真正原因。我正在分析由ieframe.dll調用的js。如果我發現任何事情,我會更新我的答案。

相關問題