最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Close window, after redirect - Stack Overflow

programmeradmin2浏览0评论

Here's my scenario:

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).

Script that opens the window:

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);

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

Any ideas?

Thanks.

Here's my scenario:

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).

Script that opens the window:

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);

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

Any ideas?

Thanks.

Share Improve this question edited Jan 8, 2012 at 0:03 BartekR 3,9873 gold badges27 silver badges33 bronze badges asked Jan 7, 2012 at 22:27 NickNick 1,2775 gold badges31 silver badges46 bronze badges 2
  • Well, you could do a $.ajax() call to get the new url, then redirect from the page itself. – Jared Farrish Commented Jan 7, 2012 at 22:41
  • Do you have access to modify the login page and the page it redirects to? I'm thinking if this is the case you could pass a querystring flag to the login page and then the login page to the redirect page so when such page loads it closes itself immediately. – epignosisx Commented Jan 7, 2012 at 22:55
Add a ment  | 

1 Answer 1

Reset to default 4

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);
发布评论

评论列表(0)

  1. 暂无评论