Javascript 打开新选项卡/窗口并关闭旧选项卡/窗口
这是我的当前代码,但是在新选项卡打开之前执行Javascript 打开新选项卡/窗口并关闭旧选项卡/窗口,javascript,jquery,internet-explorer-8,Javascript,Jquery,Internet Explorer 8,这是我的当前代码,但是在新选项卡打开之前执行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() {
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
});
$(document.body).append(“”);
$('pcl_frame').attr('src',data['url']);
$('#pcl_frame')。加载(函数(){
//加载页
document.getElementById('pcl_frame').contentWindow.CreateCBTPlayer(数据['code'],“new”);
//打开上述选项卡/窗口后关闭当前窗口
closeWindow();//有2秒的延迟
});
是否有办法等待上面的命令先打开新选项卡/窗口,然后关闭用于打开新窗口的当前选项卡
此外,我无法更改CreateCBTPlayer
函数
免责声明
我知道我所做的不是很合乎逻辑,但必须这样做才能绕过我必须使用的旧系统 我不完全确定,但我认为你可以这样做:
(function(old_window) {
$(new_window).load(function() {
old_window.close();
});
})(window);
外部自调用匿名函数用于将局部变量
old_window
绑定到当前window对象。内部函数是捕获绑定并关闭窗口的闭包。此关闭绑定到新窗口的onload
事件。因此,当加载新窗口时,它将关闭当前窗口。我不知道您想做什么,但我猜您必须在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']);
“#pcl_frame”是什么类型的元素?它是一个iFrame(更新后的帖子),你需要更具体一些,那代码到底是用来做什么的?另外,您知道
document.getElementById('pcl_frame')
在该上下文中等于this
?可能会给新窗口一个onload
处理程序来关闭旧窗口。哦,不,我对jQuery/javascript还是很陌生,谢谢。代码加载数据['url'],这是同一域上的一个url,然后当这个url在iFrame中完全加载后,我从iFrame中运行CreateCBTPlayer函数,因为这个函数就是这个函数所在的位置,这个函数然后打开一个新的选项卡,此时我想关闭当前选项卡。这个代码不起作用。在IE8中进行测试。我得到了一个错误,对象不支持属性或方法“CreateCBTPlayer”
,所以您的意思是您使用的是一个非常过时的浏览器,而在该浏览器中,您的CreateCBTPlayer函数不起作用?试着用iframe
替换this
,看看这是否解决了问题,否则问题是你的功能,而不是我的代码。相信我,如果我有选择的话,我会使用IE10,或者更好的是Chrome或Firefox。使用iframe
而不是this
会导致同样的错误。还有其他想法吗?请参阅答案中添加的解释。抱歉,但是闭包很难解释。