【问题标题】:Detect when an iframe starts to load new URL检测 iframe 何时开始加载新 URL
【发布时间】:2013-06-23 07:02:54
【问题描述】:

如何检测我页面上的 iframe开始 加载新页面?

我们的情况是:

  • 当 iFrame 内容开始发生变化时,我们需要显示“加载”动画并隐藏搜索框。
  • 我知道如何处理“iframe 已完成加载”事件(隐藏动画),但不知道如何捕捉初始“开始更改”事件...

注意: 我可以将 jquery “click” 钩子附加到菜单上的链接,这将起作用。但是,在 iframe 内容内部有许多交叉引用链接,并且“更改”事件也适用于它们!所以我们需要在 用户点击 iframe 内的链接iframe src 通过 javascript 更改 时捕获事件 - 因为我们还想显示加载动画和隐藏搜索框。

【问题讨论】:

标签: javascript jquery events iframe


【解决方案1】:

如果iframe和容器页面同源,我找到了一个更好的解决方案,不必在内页中添加额外的代码:

<iframe src="same-origin.com" onload="content_finished_loading(this)"></iframe>
<script>
    var indicator = document.querySelector('.loading-indicator');
    var content_start_loading = function() {
        indicator.style.display = 'block';
    };

    var content_finished_loading = function(iframe) {
        indicator.style.display = 'none';
        // inject the start loading handler when content finished loading
        iframe.contentWindow.onunload = content_start_loading;
    };
</script>

【讨论】:

  • 哇,非常令人印象深刻的解决方案 :) 我喜欢它!
  • 完美!好主意!
  • 正如@jbyrd 在下面的评论中提到的那样,使用iframe.contentWindow.onbeforeunload 而不是.onunload 会更早地触发回调,实际上只是在发送新请求时。
  • 请注意,这仅在 iframe contentWindow 与尝试设置事件侦听器的脚本具有相同来源时才有效。
【解决方案2】:

我想出了以下解决方案——这只是因为我们控制了 iframe 内容和主机窗口的内容

在 iframe 中,我们将以下脚本添加到页面页脚(所有页面使用相同的模板,因此这是对单个文件的更改)

<script>
window.onunload = function() {
    // Notify top window of the unload event
    window.top.postMessage('iframe_change', '*');
};
</script>

在主机窗口中,我们添加这个脚本来监控 iframe 状态

function init_content_monitor() {
    var content = jQuery('.iframe');

    // The user did navigate away from the currently displayed iframe page. Show an animation
    var content_start_loading = function() {
        alert ('NOW: show the animation');
    }

    // the iframe is done loading a new page. Hide the animation again
    var content_finished_loading = function() {
        alert ('DONE: hide the animation');
    }

    // Listen to messages sent from the content iframe
    var receiveMessage = function receiveMessage(e){
        var url = window.location.href,
            url_parts = url.split("/"),
            allowed = url_parts[0] + "//" + url_parts[2];

        // Only react to messages from same domain as current document
        if (e.origin !== allowed) return;
        // Handle the message
        switch (e.data) {
            case 'iframe_change': content_start_loading(); break;
        }
    };
    window.addEventListener("message", receiveMessage, false);

    // This will be triggered when the iframe is completely loaded
    content.on('load', content_finished_loading);
}

【讨论】:

  • 不错的解决方案,它对我有帮助。我为此更改了 window.onunload 语句: $(window).on('beforeunload ',function() { window.top.postMessage('iframe_change', '*'); });这对我来说要快得多。
  • 在我看来,这过于复杂,并且忽略了关键事件应该是onbeforeunload,而不是onunload。 OP 明确表示他想知道框架何时开始 加载新的 url。见stackoverflow.com/a/30719095/422845
  • 如果我正在加载的 URL(在 iFrame 中)来自不同的域,这会起作用吗?
  • @YugandharPathi 是的。注意postMessage 方法的第二个参数是'*',这意味着messa 被发布到顶部窗口上下文,而不管它托管它的域是什么。所以 iframe 可以托管在其他任何地方。
  • 顺便说一句非常好的解决方案。这对我帮助很大,我还需要知道 iframe 何时开始加载新 URL,这非常适合我的特定场景。
【解决方案3】:

已经有一个可能的解决方案:iFrame src change event detection?

但是,如果您想操作目标页面,则无需触摸 iframe-target 文件内容。只需在父页面中添加正确的 JS 代码,它就可以访问 SAME-ORIGIN iframe。我用过这样的东西:

<iframe id="xyz" ....>
.....
<script>
var iframeElement = document.getElementById("xyz");
var iframe_El = iframeElement.contentDocument || iframeElement.contentWindow.document;
// manipulate iframe_El wih "on complete" events and like that.
....
</script>

更多信息:https://jsfiddle.net/Lnb1stc8/

【讨论】:

    【解决方案4】:

    当您动态创建 iframe 时,像这样包含 ONLOAD 事件...

    F=document.createElement('iframe');
    F.onload=function(){
        UpdateDetails(
            this.contentWindow.document.title,
            this.contentWindow.location
        );
    };
    F.src='some url';
    document.getElementById('Some_Container').appendChild(F);
    

    随着用户上网,onload 事件现在将通过下面的函数不断更新下面的全局变量:

    var The_Latest_Title='';
    var The_Latest_URL='';
    function UpdateDetails(Title,URL){
        The_Latest_Title=Title;
        The_Latest_URL=URL;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2014-07-09
      • 2013-06-14
      • 2011-07-30
      • 2012-05-22
      • 2012-02-16
      • 2012-10-24
      相关资源
      最近更新 更多