Javascript JS,如何让一个页面作为iframe加载到另一个页面中?

Javascript JS,如何让一个页面作为iframe加载到另一个页面中?,javascript,iframe,Javascript,Iframe,我有两个页面,一个是index.html另一个是iframe.html 如何让iframe.html仅在index.html中加载 如果iframe在其他页面中或直接键入http://www.site.com/iframe.html在浏览器中,页面仅显示一些单词,如opps~~有问题。 类似于bing shopping的东西 谢谢。您可以使用窗口.parent.location获取iframe中的父url并相应地执行操作。如果未定义window.parent,则可能意味着直接调用了iframe.

我有两个页面,一个是
index.html
另一个是
iframe.html

如何让
iframe.html
仅在
index.html
中加载

如果iframe在
其他页面中
或直接键入
http://www.site.com/iframe.html
在浏览器中,页面仅显示一些单词,如
opps~~有问题。

类似于bing shopping的东西


谢谢。

您可以使用
窗口.parent.location
获取iframe中的父url并相应地执行操作。如果未定义window.parent,则可能意味着直接调用了iframe.html。如果定义了该值,请检查该值,并确保它来自您域上的/index.html。

我认为这样做应该可以

var inIframe = window.location != window.parent.location;

您可以在iframe.html中使用此javascript:

if (top.location.href != 'http://www.site.com/index.html') {
    // the proper redirect here
    top.location.href = 'http://www.site.com/index.html';
}
这样,它将始终作为i帧加载

if( self == top )
 location.href='/';

将其放入iframe页面

Diallo,尝试使用您提供的内容,但当打开
index.html
时,页面将始终刷新。实际上,当您打开index.html时,页面不应刷新,如果页面不同,则条件仅为
true
。此代码位于iframe.html页面内部,而不是索引。html@simshaun,@Darin Dimitrov,
window.parent.location
只需将
iframe.html
重定向到
index.html
,但我需要一些像
bing shopping
@yuli chika这样的效果,
window.parent.location
不会重定向任何位置:它会提供父托管页面的位置(如果有)。如果修改该值,则它将重定向。但在这里,我们谈论的是阅读它,并根据它的价值采取相应的行动。顺便说一下,这是
iframe.html
了解父容器的唯一方法。