javascript iframe 卸载

标签 javascript html events node.js iframe

我的页面中有一个 iframe,该 iframe 嵌入了一个不在我的服务器上的网站。

我正在寻找一种在 iframe 重定向之前触发函数的方法。例如,当用户单击 iframe 内的链接并且 iframe 正在重定向时,我想在重定向之前触发一个函数。

到目前为止我有这个代码:

document.getElementById("ifm").addEventListener("unload", trig());
function trig(){
    alert('2');
}

此代码仅在 iframe 首次加载时发出警报。

我也尝试过“onbeforeunload”事件,它给出了相同的结果。

问题:如何在 iframe 重定向之前发出警报?

最佳答案

您正在调用 trig 函数,因此返回结果就是传递给 addEventListener 函数的内容。

将代码更改为..

document.getElementById("ifm").addEventListener("unload", trig);

document.getElementById("ifm").addEventListener("unload", function() { trig(); });

iframe是否支持unload事件,我不知道......

谢谢

关于javascript iframe 卸载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280361/

相关文章:

javascript - 在特定的 div 类中单击时提交 Jquery 文本和表单

javascript - 是否有一个 JavaScript 物理库可以处理 HTML 元素(例如 div)而不是 Canvas ?

html - IE9 输入/按钮元素边框颜色问题

javascript - 将事件传递给父级时,事件阻止 ReactJS 中的默认设置

javascript - 错误: socket hang up using https

javascript - 在 ng-if AngularJS 中使用 ng-repeat

javascript - JQuery 和 frame(set)s - 为什么它们消失了?

macos - OSX Quartz 事件点击 : event types and how to edit events

c# - 手动触发按钮(或任何控件)单击事件。 C#

javascript - 使用Jquery,按下标签<a>时如何将表格传递到另一个页面?