开发者

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

I have two pages, one is index.html another is iframe.html

How to let the iframe.html only be loaded in开发者_JAVA技巧 index.html?

If iframe in other pages or directly type http://www.site.com/iframe.html in browser, the page only show some words like opps~~something is wrong.

something like bing shopping.

Thanks.


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

Put this in the iframe page


You could use the window.parent.location to fetch the parent url inside the iframe and act accordingly. If window.parent is not defined it probably means that the iframe.html was called directly. If it is defined inspect the value and ensure it comes from /index.html on your domain.


I think this should do it...

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


You can use this javascript in your iframe.html:

if (top.location.href != 'http://www.site.com/index.html') {
    // the proper redirect here
    top.location.href = 'http://www.site.com/index.html';
}

this way it will always be loaded as an i frame

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜