Leos Literak
Leos Literak

Reputation: 9406

Redirect / reload browser

I want to display popup window that closes once user is auhenticated and redirects user to home page. For backward compatibility login page can display in browser and not in popu window.

index.jsp

<%--@elvariable id="USER" type="cz.literak.demo.oauth.model.entity.User"--%>
<c:if test="${not empty USER}">
    <p>
        Logged as ${USER.firstName} ${USER.lastName}, <a href="logout">Logout</a>
        <c:if test="${not USER.areRegistered('TW,FB,GG')}">
            <a href="login.jsp" class="popup" data-width="600" data-height="400">Improve Login</a>
        </c:if>
    </p>
</c:if>
<c:if test="${empty USER}">
    <p>
        <a href="login.jsp" class="popup" data-width="600" data-height="400">Login</a>
    </p>
</c:if>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>
<script>
    $("a.popup").click(function(event){
        event.preventDefault();
        window.open (href, "OAUTHLOGIN", "height=" + height +",width=" + width + "");
    });
</script>

Once authenticated, popup/browser is redirected to logged.jsp. It shall redirect main window to home page and close popup window, if login.jsp was opened in it.

<script>
    window.top.location = "index.jsp";
    if (window.name == "OAUTHLOGIN") {
        window.close();
    }
</script>

It seems to be working with one exception. If browser already displays index.jsp, then nothing happens. I tried location.reload(true), but it caused infinite loop in popup instead.

How can I make it work? Thanks

Upvotes: 0

Views: 662

Answers (1)

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

You can use;

<script>
    // Get parent url
    var parent_url = window.parent.location.href;
    // Check parent if it has "index.php"
    if (parent_url.match(/index.jsp/g) || parent_url.match(/.com\//g)) { // yoursite.com/
        // if it is, reload parent window
        window.opener.location.reload(false);
    } else {
        // Else go to index.php
        window.top.location = "index.jsp";
    }

    if (window.name == "OAUTHLOGIN") {
            window.close();
    }
</script>

Edit: Another possible solution: When user logged in, just refresh parent window. In your system, you need to check if user logged in or not in an interceptor like something. When parent window refreshed, if user logged in, it will be redirected to registered area, else it will redirected to login page if current page is registered page. In your loggedin.jsp, you can only use;

<script>
    window.opener.location.reload(false);
    if (window.name == "OAUTHLOGIN") {
            window.close();
    }
</script>

Upvotes: 1

Related Questions