2013-03-29 85 views
24

我有这样的iframe代码:加载iframe链接到父窗口?

<iframe src="http://www.example.com" border="0" framespacing="0" marginheight="0" marginwidth="0" vspace="0" hspace="0" scrolling="yes" width="100%" frameborder="0" height="100%"></iframe> 

我想要做的is..when我点击iframe中的任何链接page..then我会去,并在页面将加载,我会getout从当前页面到iframe链接作为新页面。

类似于target="_self" - 不像正常的iframe,它会在没有任何浏览器本身变化的情况下进入该链接。

例子: IFRAME SRC:http://www.w3schools.com

当我点击 “学习HTML” 的iframe中page..then我会去http://www.w3schools.com/html/default.asp 和我的浏览器的URL会改变它了。

所以我只是点击了iframe中的链接,我去了它。

任何想法?

回答

35

是的,您可以使用target="_parent"来实现此目的。

“target =”_ parent“在父框架中打开链接的文档。”

例子:

<a target="_parent" href="http://example.org">Click me!</a> 

编辑:

如果不工作,你可以尝试_top

<a target="_top" href="http://example.org">Click me!</a> 

或者base target

<base target="_parent" /> 

或者window.top.location在JavaScript:

window.top.location = "http://example.com"; 
+0

Q具有了updated..thank你。 – user2203703

+0

@ user2203703我不明白,这回答了这个问题。您在iframe中的链接上使用目标父级.. – lifetimes

+0

我想离开我的页面,其中有iframe和goout到我在iframe中单击的链接 – user2203703

13

你在找什么是<a href="..." target="_parent">

_parent将导致它去父框架

_top将导致它达到最高级别。

_parent_top应该适合您的目标。

+1

也许我对此问题并不清楚。 iframe中的页面...是否可以控制该页面中的HTML,还是第三方网站?我更新了我的答案,以包含target = _top,但当然这是假设您可以控制帧的内容。 –

5

是的,只需在链接上添加target="_parent"并加载到主页面上即可。

+0

Q已更新..谢谢。 – user2203703

6

您可以使用javascript将目标动态添加到链接,如下所示。

function onLoadIF(frame) 
{ 
    var elements = frame.getElementsByTagName('a'), 
     len = elements.length; 

    while(len--) { 
     elements[len].target = "_parent"; 
    } 
} 

然后将onload =“onLoadIF(this)”添加到iframe中。

1

如果网站在iframe中打开然后重定向到主站点

<script> 
    if (window.top.location != window.self.location) { 
     top.window.location.href = window.self.location; 
    } 
</script> 
+0

这是一个黑客,它甚至不回答这个问题。 –