如何使用.unload()或.onbeforeunload()动态创建iframe

如何使用.unload()或.onbeforeunload()动态创建iframe,iframe,mouseleave,Iframe,Mouseleave,当窗口离开或刷新或在同一选项卡中输入新url时,我需要显示警报。在这里,我使用jquery创建了iframe。在那个iframe中,我编写了卸载事件。但它不起作用 document.getElementById("#ioi_widget_674").addEventListener("unload", function(){ //action here alert("test"); }); 当我离开窗口时,我需要在iframe中显示警报

当窗口离开或刷新或在同一选项卡中输入新url时,我需要显示警报。在这里,我使用jquery创建了iframe。在那个iframe中,我编写了卸载事件。但它不起作用

document.getElementById("#ioi_widget_674").addEventListener("unload", function(){  //action here  alert("test"); });
当我离开窗口时,我需要在iframe中显示警报