iframe 中的跨帧脚本

Cross Frame Scripting in iframes

本文关键字:脚本 iframe      更新时间:2023-09-26

我有一个jsp页面。它有 3 个 i 帧。我遇到了跨帧脚本的问题。我可以将页面从任何其他域加载到我的一个 i-frame。你能告诉我如何克服这个问题吗?我尝试了以下代码:

             <style>
                html{display : none ; }
            </style>
            <script>
                if( self == top ) {
                    document.documentElement.style.display = 'block' ;
                } else {
                    top.location = self.location ;
                }

            </script>

我还尝试了一个过滤器,它添加了标题"X-框架选项",同源

两者都不起作用。

对于加载到 iframe 中的来自不同来源的 html 页面,您无法访问该页面的窗口或任何其他对象。

为了在不同来源加载到 iframe 中的 html 页面之间进行通信,您必须使用"postMessage"函数。有关javascript中的详细信息和示例,您可以获得大量教程。

试试这个脚本,它不允许你的页面在其他域的 iframe 中使用。

function bust() {
    var urlRefer = (window.location != window.parent.location) ? document.referrer: document.location;
    var envName = window.location.hostname;
    var envNameNew = new RegExp(envName);
    if (!(envNameNew.test(urlRefer))) {
        window.top.location="http://"+envName;  
    }
}
bust();