打开新选项卡/窗口并关闭旧选项卡 [英] Open new tab/window and close old one

查看:142
本文介绍了打开新选项卡/窗口并关闭旧选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的代码,但 closeWindow 函数在新标签页打开之前执行。

Here is my current code, but the closeWindow function is executed before the new tab opens.

    $(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
}); 

有没有办法等待上面的命令先打开新的标签/窗口,然后关闭用于打开新窗口的当前选项卡。

Is there a way to wait for the command above to open the new tab/window first, then close the current tab that was used to open the new window.

此外,我无法更改 CreateCBTPlayer 函数。

Also I can't change the CreateCBTPlayer function.

我知道我在做什么是不合逻辑的,但必须要做绕过我必须使用的旧系统。

I know what I'm doing isn't very logical, but it has to be done to get around an old system I have to work with.

推荐答案

我不完全确定,但我认为你可以这样做:

I'm not totally sure, but I think you can do something like this:

(function(old_window) { 
    $(new_window).load(function() {
         old_window.close();
    });
})(window);

外部自调用匿名函数用于绑定局部变量 old_window 到当前窗口对象。内部函数是一个闭包,捕获绑定并关闭窗口。此闭包绑定到新窗口的 onload 事件。因此,当加载新窗口时,它将关闭当前窗口。

The outer self-calling anonymous function is used to bind the local variable old_window to the current window object. The inner function is a closure that captures that binding and closes the window. This closure is bound to the new window's onload event. So when the new window is loaded, it will close the current window.

这篇关于打开新选项卡/窗口并关闭旧选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆