redirect if the page is iframe

落爺英雄遲暮 提交于 2019-12-12 01:42:33

问题


Here is what i want to do:

in my blog, comments are shown in a fancybox popup using iframe. (eg:- for the post number 2, comments link open an iframe fancybox comments.php?id=2)

I want to check whether comments.php is loaded in an iframe and if not, redirect to the respective blog page. i.e. post.php?id=2

I checked Google and other sites incl. stackoverflow. but no use. pls help. (this seems to be easy. but it ain't working http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/


回答1:


Put this script in your page. If it is not opened in a frame, it will redirect to post.php?id=...

<script type="text/javascript">
    if (parent.location==location) {
        location.href = "post.php" + location.search;
    }
</script>



回答2:


I am thinking something like

if ( document.parent.frames.length > 0 ) {
    document.parent.location = "post.php";
}

should work



来源:https://stackoverflow.com/questions/12248940/redirect-if-the-page-is-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!