确保页面总是在iframe中加载


Make sure that page always gets loaded in an iframe

我使用第三方预订系统在一个iframe在我的网站。当用户进行预订时,他们会收到一封带有确认链接的电子邮件,该链接直接指向预订页面(我通常在iframe中显示)。因此,用户只能看到日历,而不是在iframe中看到日历的整个页面。

是否有一种方法来检查页面是否在iframe内加载,如果不是,加载周围的父页面?

希望你明白:

iframe-page.html

<script>
    if ( window.top == window.self ) {
        location.replace('site.html?path=' + encodeURIComponent(location.href));
    }
</script>

site.html

<iframe id="iframe"></iframe>
<script>
    // site.html?path=lol.html -> lol.html
    var path = location.href.split('?').pop().split('path=').pop().split('&').shift(),
        iframe = document.getElementById('iframe');
    if ( path ) {
        iframe.setAttibute('src', decodeURIComponent(path));
    } 
</script>

但是你可能会遇到同源策略的问题

在javascript中 * 窗口。top*需要像窗口一样,但在iframe窗口中不等于window.top.

<script>if( window.top != window ) window.top.location = 'PARENT_URL';</script>