关闭窗口,重定向后 [英] Close window, after redirect

查看:97
本文介绍了关闭窗口,重定向后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方案:

我正在从javascript文件打开一个窗口,并设置一个间隔以检查该窗口是否已关闭。 (这可以作为各种回调函数)。

I'm opening a window from a javascript file, and setting an interval to check whether that window has been closed or not. (this acts as a callback function of sorts).

问题是,我打开的窗口是一个包含重定向的页面。所以我在某种程度上需要一种方法让窗口在重定向发生后关闭(因此可以触发回调)。

The problem is, the window I'm opening is to a page that contains a redirect. So I somehow need a way to have the window close itself after the redirect has taken place (so the callback can be triggered).

打开窗口的脚本:

oauthWindow   = window.open("login/index.php", "Login", options.windowOptions);
oauthInterval = window.setInterval(function() {
    if (oauthWindow.closed) {
        window.clearInterval(oauthInterval);
        options.callback();
    }
}, 1000);

正在打开的页面将重定向:

The page that's being opened will redirect itself:

 $url = $oauth->getAuthorizeURL($request['oauth_token']);
 header("Location: " . $url);

我需要一种方法让窗口在重定向发生后自行关闭。 (window.close());

I need a way for the window to close itself after that redirect has occurred. (window.close());

任何想法?

谢谢。

推荐答案

您可以检查新打开的窗口的window.location.href,只要它与登录页面不同,您就知道重定向已经发生,关闭它是安全的。这是一个例子:

You could check the window.location.href of the newly open window and whenever it is different from the login page you know that the redirect has taken place and it is safe to close it. Here is an example:

var win = window.open("/login/index.php", "Login");
var id = setInterval(function () {
    if (win.location.href.indexOf("/login/index.php") < 0) {
        clearInterval(id);
        //ready to close the window.
    }
}, 500);

这篇关于关闭窗口,重定向后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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