2011-03-13 110 views
0

对不起,粗略的标题。我的文档中有一个iframe。我希望在某些情况下,iframe会破坏自己成为父(主)文档。例如,运行以下代码将iframe文档转换为父文档

var el = document.createElement("iframe"); 
el.setAttribute('id', 'ifrm'); 
document.body.appendChild(el); 
el.setAttribute('src', 'http://www.gmail.com'); 

经过一段时间或登录后,iframe消失并取代主文档。我怎样才能获得相同的行为?

回答

0

即使这是可能通过一些复杂的JavaScript的黑客,我不知道这是否是可取的。如果用户点击后退按钮,会发生什么情况?您现在是否尝试将所有内容都放回到iframe中并恢复原始文档?

我认为,要保持大多数人习惯的方式和最可用的方式,最好将父窗口重定向到您想要转到的src url。另外,一旦用户在iframe中的某个位置导航,您将不再知道用户所在的位置,除非它位于同一个域中。这是一个安全限制,没有办法绕过它。

这个怎么样?

//From inside the iframe to set parent to gmail.com 
window.top.location.assign('http://www.gmail.com') 

//From inside the iframe to set parent to the current iframe location 
window.top.location.assign(window.location.href) 

//From the parent document to gmail.com 
window.location.assign('http://www.gmail.com') 

//From the parent document to the iframe location (if iframe is same domain) 
window.location.assign(window.frames[0].location.href)