我有两个页面,一个是index.html
另一个是iframe.html
JS,如何让页面只能作为iframe加载到另一个页面?
如何让iframe.html
在index.html
只能加载?
如果IFRAME在other pages
或直接键入浏览器http://www.site.com/iframe.html
,页面仅显示一些词像opps~~something is wrong.
像bing shopping
。
谢谢。
我有两个页面,一个是index.html
另一个是iframe.html
JS,如何让页面只能作为iframe加载到另一个页面?
如何让iframe.html
在index.html
只能加载?
如果IFRAME在other pages
或直接键入浏览器http://www.site.com/iframe.html
,页面仅显示一些词像opps~~something is wrong.
像bing shopping
。
谢谢。
您可以使用window.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帧
Diallo,尽量使用你给的东西,但是当打开'index.html'时,实际上当你打开index.html的时候页面总会刷新 – 2011-05-03 21:36:58
它不应该刷新,如果页面不同,条件只是'true' 。此代码进入iframe.html页面而不是index.html – Ibu 2011-05-03 22:02:12
if(self == top)
location.href='/';
把这个在iframe页面
@s imshaun,@Darin Dimitrov,'window.parent.location'只是将'iframe.html'重定向到'index.html',但我需要一些效果,比如'bing shopping' – 2011-05-03 21:48:41
@yuli chika,'window.parent.location' doesn不会重定向到任何地方:它会为您提供父托管页面的位置(如果有)。如果您修改该值,则会重定向。但在这里我们正在讨论阅读它,并根据其价值作出相应的行动。顺便说一句,这是'iframe.html'知道父容器的唯一方法。 – 2011-05-03 21:50:52