是否有人知道使代码无法正常工作(' crosswindow')的最佳解决方案,以跟踪openNew()函数打开的多个窗口?
它目前只能跟踪一个窗口。我尝试了一些带有对象的东西,但我不是专家。欢迎提出所有建议。
使用Javascript:
function openNew(href){
timer = setInterval('polling()',500);
win = window.open(href, '_blank');
url=href;
openTime=0;
clickTime= (new Date()).getTime();
}
function polling(){
if (win && win.closed) {
var closingTime = (new Date()).getTime();
var diff = ((closingTime-clickTime)/1000);
clearInterval(timer);
console.log(url+" closed after " + diff + " secs");
}
}
HTML 的
<a href="http://google.com" onClick="openNew('http://google.com'); return false;" target="_blank">google</a>
<a href="https://facebook.com" onClick="openNew('https://facebook.com'); return false;" target="_blank">facebook</a>
目标最终是能够从父页面打开多个新窗口,当子窗口关闭时,它会记录在父窗口中。
我用笔模拟了控制台(以避免无限循环警报) http://codepen.io/anon/pen/EjGyQz?editors=101
答案 0 :(得分:2)
所以,你只需要在闭包中包含对轮询的调用,并将所有变量传递给它,因此它不会覆盖旧的变量:
http://codepen.io/anon/pen/GJPqwm?editors=101
function openNew(href){
var current = window.open(href, '_blank');
var clickTime= (new Date()).getTime();
(function(win, url, clickTime) {
var timer = setInterval(function() { polling(win, timer, url, clickTime) },500);
})(current, href, clickTime);
}
function polling(win, timer, url, clickTime){
if (win && win.closed) {
var closingTime = (new Date()).getTime();
var diff = ((closingTime-clickTime)/1000);
clearInterval(timer);
//replace console with div log
document.getElementById('log').innerHTML += url+" closed after " + diff + " secs<br>";
}
}