【问题标题】:Get a page redirect with jquery使用 jquery 获取页面重定向
【发布时间】:2014-05-08 18:01:34
【问题描述】:

我有一个 iFrame,它在 iFrame 内的页面上有多个链接。

从外部文档中,如何判断 iFrame 中的页面是否被重定向?

<body>
    <script>
        if(somePage.html has been redirected) {
            //do something
        }
    </script>

    <iframe src="somePage.html">...</iframe>
</body>

【问题讨论】:

  • 这一切都发生在同一个域中,iFrame中没有跨域站点
  • @adeneo 新页面将来自新域,但 somePage.html 在我的域中。
  • 那我不认为你可以,当 iframe 内的链接发生变化时,源不会改变,所以我假设必须访问 iFrame 并获取位置,但 iframe 带有交叉无法访问域内容。也许有人有一个聪明的方法来做到这一点?
  • @adeneo 所以你是对的,当我尝试获取新页面的正文元素时出现安全错误。但就我而言,我需要做的就是获取新页面的高度,以便调整 iFrame 高度。尽量避免滚动条以保持清洁
  • @adeneo 无论如何都可以在 iframe 中获取页面的高度,而不管域来源如何?

标签: javascript jquery ajax iframe


【解决方案1】:

给 iframe 一个 id 并尝试将 load() 函数放入 document.ready() 处理程序中:

$(document).ready(function () {
    $("iframeID").load(function () {
        //iframe loaded
        alert(this.contentWindow.location);
    });
});

【讨论】:

  • 它可以工作...但是当我尝试从该页面获取元素时出现安全错误。
  • 1:Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match. 2:Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "https://docs.google.com". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "https". Protocols must match.
  • 我完全理解错误,并且超出了这个问题的范围。但我不相信我想做的事能做到。
  • 我明白了。好吧,我认为@adeneo 在他上面的评论中提到的是合法的。无法访问跨域内容,此解决方案依赖于“了解”当前页面。
  • 您的解决方案实际上 100% 有效。如果加载了新页面,我可以捕获该负载。所以我认为要解决我的身高问题,我最终要做的就是在加载新页面时向 iFrame 添加滚动条
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
相关资源
最近更新 更多