Detecting when an iframe gets or loses focus

前端 未结 9 1379
我寻月下人不归
我寻月下人不归 2020-12-31 06:24

What\'s the correct way of detecting when an iframe gets or loses focus (i.e. will or will not receive keyboard events)? The following is not working in Fx4:



        
9条回答
  •  臣服心动
    2020-12-31 06:53

    The solution is to inject a javascript event on the parent page like this :

    var script = document.createElement('script');
    script.type = 'text/javascript';
    
    script.innerHTML = 
    "document.addEventListener('click', function()" + 
    "{ if(document.getElementById('iframe')) {" +
        // What you want
    "}});"; 
    
    head.appendChild(script);
    

提交回复
热议问题