2014-02-27 137 views
0

我想要显示弹出式窗口,一旦用户进行了身份验证并关闭用户重定向到主页,即会关闭。为了向后兼容登录页面可以在浏览器中显示,而不是在popu窗口中显示。重定向/重新加载浏览器

的index.jsp

<%[email protected] 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> 

一旦通过验证,弹出/浏览器重定向到logged.jsp。如果在其中打开login.jsp,它应该将主窗口重定向到主页面并关闭弹出窗口。

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

它似乎只有一个例外。如果浏览器已经显示index.jsp,则什么都不会发生。我试过location.reload(true),但它在弹出窗口中导致了无限循环。

我该如何让它工作?谢谢

+0

does window.top.location.reload work? – Huangism

+0

否,弹出关闭,但原始页面不受影响。 –

+0

我认为有迹象表明您可以判断页面是否已刷新?就像用户登录时,它显示了他们的用户名或类似 – Huangism

回答

1

您可以使用;

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

编辑: 另一种可能的解决方案: 当用户登录时,只刷新父窗口。在你的系统中,你需要检查用户是否登录或者不在拦截器中。当父窗口刷新时,如果用户登录,它将被重定向到注册区域,否则如果当前页面是注册页面,它将被重定向到登录页面。在您的loggedin.jsp中,您只能使用;

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

我会尝试,但如果条件必须不同。 url不需要包含index.jsp,斜线/也是可能的。 –

+0

可能有2个选项对吧?查看我的更新回答 –

+0

匹配方法中缺少括号。尽管如此,父窗口不刷新。你可以试试它:http://www.literak.cz:8080/OAuthLogin/ –