这是我当前的代码,但closeWindow
函数在新标签页打开之前执行。
$(document.body).append('<iframe width="500px" height="500px" id="pcl_frame" style="display:none;"></iframe>');
$('#pcl_frame').attr('src', data['url']);
$('#pcl_frame').load(function() {
// Load page
document.getElementById('pcl_frame').contentWindow.CreateCBTPlayer(data['code'],"new");
// Close current windows after the above tab/window opens
closeWindow(); // has a 2 second delay
});
有没有办法等待上面的命令先打开新标签/窗口,然后关闭用于打开新窗口的当前标签。
此外,我无法更改CreateCBTPlayer
功能。
我知道我正在做的事情不是很合乎逻辑,但必须要做到解决我必须要处理的旧系统。
答案 0 :(得分:1)
我不完全确定,但我认为你可以这样做:
(function(old_window) {
$(new_window).load(function() {
old_window.close();
});
})(window);
外部自调用匿名函数用于将局部变量old_window
绑定到当前窗口对象。内部函数是一个闭包,捕获绑定并关闭窗口。此闭包绑定到新窗口的onload
事件。因此,当加载新窗口时,它将关闭当前窗口。
答案 1 :(得分:1)
我不知道您要尝试做什么,但我猜测您必须在onload函数之后设置源以使其正常启动,并且因为load()
已被弃用且无论如何你都在使用原生对象,为什么不在普通的js中做到这一点:
var iframe = document.createElement('iframe');
iframe.width = '500px';
iframe.height = '500px';
iframe.id = 'pcl_frame';
iframe.style.display = 'none'; //note that not all browsers load hidden content
iframe.onload = function() {
this.contentWindow.CreateCBTPlayer(data['code'],"new");
closeWindow();
}
$('body').append(iframe).attr('src', data['url']);