简体   繁体   English

我的网页内的IFrame触发模糊事件

[英]IFrame inside my webpage triggering blur event

I have a countdown on my webpage along with an IFrame. 我的网页上有倒计时和IFrame。 Countdown will stop on $(window).blur and start on $(window).focus . 倒计时将停在$(window).blur并从$(window).focus When i click on the IFrame, it triggers the blur event and stops the countdown. 当我点击IFrame时,它会触发模糊事件并停止倒计时。 I have solved this issues by following code line. 我通过以下代码行解决了这个问题。

 $(window).blur(function () {
  if (document.activeElement.nodeName == "IFRAME") {
    //Continue timer
    else {
        //Stop timer
    }
 });

But, When i click outside the window immediately after clicking inside IFrame, Blur event is not getting triggered. 但是,当我在点击IFrame内部后立即点击窗口外时,Blur事件不会被触发。 Since blur event will not be triggered after a blur event unless focus event is triggered. 由于模糊事件不会在模糊事件后触发,除非触发焦点事件。

I have referred similar topics - $(window).blur event affecting Iframe and How can I capture the blur and focus of the entire browser window? 我引用了类似的主题 - 影响Iframe的$(window).blur事件以及如何捕获整个浏览器窗口的模糊和焦点?

Any other Ideas? 还有其他想法吗?

Update - I did an another approach which lead me another step ahead. 更新 - 我做了另一种方法,让我领先一步。 The following code is new approach 以下代码是新方法

$(window).on('focus', function () {
                            //Coundown runs
                    }).on('blur', function () {
                            //Coundown stops
                    });;

 $(document).ready(function () {
                var iframe = document.getElementById("iframeID");
                            $(iframe.contentWindow).on('focus', function (){
                                    //Coundown runs
                            }).on('blur', function () {
                                    //Coundown stops
                            });
                    });

This code will make countdown run on iframe focus and stop on iframe blur. 此代码将使iframe焦点上的倒计时运行并停止iframe模糊。

But , the issue i am facing in this approach is countdown working fine when "scr" not loaded in Iframe. 但是 ,我在这种方法中面临的问题是,当“scr”未在Iframe中加载时,倒计时工作正常。 if the "scr" is loaded in Iframe then focus and blur event for Iframe are not getting bind and countdown stops on Iframe click. 如果在iframe中加载“scr”,则iframe的焦点和模糊事件未获得绑定,并且在iframe点击时停止倒计时。

Any idea on this? 有什么想法吗?

I think the problem is, that as soon as content is loaded to your iframe it is isolated from the window by your browser (due to security conciderations). 我认为问题是,只要内容被加载到您的iframe,它就会被您的浏览器与窗口隔离(由于安全性方面的考虑)。

You could use Window.postMessage() to communicate events between the main window and the iframe. 您可以使用Window.postMessage()在主窗口和iframe之间传递事件。

Have a look at Window postmessage 看一下Window postmessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM