Window events lost after removing iframe

I have a problem when the form is embedded in the iframe and after the form is submitted, the iframe is removed from the DOM. Immediately after the form has been deleted (the form was the last thing to focus on), I can’t detect events associated with the root window element.

The frame is loaded from a separate domain, although it does not matter for this example, it is only important that I do not control the scripts on this page.

I understand that I cannot detect DOM events in an iframe, but all events are lost until the user closes the DOM after removing the iframe. This happens in both Firefox and Chrome . It seems that IE will return focus back to the original DOM, as expected. I have not tested in other browsers.

 $(window).keydown(function(e){
    console.log (e.keyCode);   
 });

var $iframe = $("<iframe src='www.example.com'>");
$("body").append($iframe);

window.setTimeout(function(){
 $iframe.remove();
}, 1000);

( , , http://codepen.io/anon/pen/WQroqe)

-
", iframe".

, iframe,
iframe , DOM .
DOM , .

Use Case: Form iframe , DOM. .

: , DOM "", , ? , DOM (, iframes). firefox, chrome. , , , , , .

( - MutationObserver iframe . , setInterval, iframe. , ).

+4
1

iframe, , $(window).focus();,

, ,

  $iframe.load(function(){
    window.setTimeout(function(){
      console.log("deleting");
      $("#deleteMe iframe").remove();
      $(window).focus(); // <======
    }, 5000);  
  });
0

Source: https://habr.com/ru/post/1607352/


All Articles