2014-02-27 136 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/ –