如何让一个页面只作为iframe加载到另一个页面

JS, How to let a page only be loaded in another page as an iframe?

本文关键字:iframe 加载 另一个 一个      更新时间:2023-09-26

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

如何让iframe.html只在index.html中加载?

如果iframe在other pages或直接在浏览器中输入http://www.site.com/iframe.html,页面只显示opps~~something is wrong.等字

bing shopping

谢谢。

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

放到iframe页面

您可以使用window.parent.location来获取iframe内的父url并采取相应的操作。如果窗口。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帧