2013-04-18 23 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。如果我发现任何事情,我会更新我的答案。

相关问题