在您的开启窗口消息回调中,您可以像这样向子窗口发送回复:
function yourMessageCallback(event) {
// your other handler stuff here...
event.source.postMessage('Yeah I got it', event.origin);
}
然后你可以在发送端做一个计时器,当回复到达时你可以清除它:
// do your postmessage here
function notReceived() {
// do stuff if the message didn't go through
}
var messageTimer = setTimeout(notReceived, 500); // 500ms should be enough for everyone?
window.addEventListener('message', function(event) {
// do necessary origin checks first etc... (not shown here)
if (event.data == 'Yeah I got it') {
clearTimeout(messageTimer);
// do stuff if the message went through
}
}, false);
我知道这可能是一个有点骇人听闻的解决方案,但可能不如用户代理嗅探那么简单?