我想在卸载页面之前,在iframe内执行一个函数,将数据发送到我的分析工具。但是我在iframe中。
我确实只知道。
window.onbeforeunload = function(){ ... }
但似乎在iframe内是行不通的,我的函数没有被执行。
我还发现了这个。
var iframe = document.getElementById('iframe');
iframe.load(
function(){
iframe.contentWindow.onbeforeunload = function(){
myFunction();
}
}
)
但也没有用
有什么其他的方法吗?
试试这个。
var iframe = document.getElementById('iframe');
iframe.addEventListener( "load", function(e) {
alert('content loaded');
});
iframe.addEventListener( "beforeunload", function(e) {
alert('content reloaded');
myFunction();
});