2009-10-01 84 views
1

單擊鏈接時,我想在重定向到目標頁面之前清除php會話。ajax的jquery延遲鏈接重定向

我想這樣做的方式是通過加載一個PHP腳本,在原始頁AJAX中清除會話,一旦該腳本加載,重定向到目標頁面。

這裏是我的代碼:

$(document).ready(function() { 
    $("a").click(function(){ 
     var clearSession = "clearsession.php"; 
     $("#content").load(clearSession,""); 
    }); 
}); 

<a href="destination.html"> 

目前,它似乎遵循該鏈接的PHP腳本完成加載之前。

幾個原則:

  • 你不能使鏈接點不是「destination.html」
  • 任何其他頁面不能變量添加到目標頁面(destination.html?清除)
  • 回答

    2

    使用load callback,因此您可以在收到響應後執行重定向。

    $(document).ready(function() { 
        $("a").click(function(){ 
         var clearSession = "clearsession.php"; 
         $("#content").load(clearSession,"",function(){ 
          window.location = $(this).attr("href"); 
          return false; 
         }); 
        }); 
    }); 
    
    <a href="destination.html"> 
    
    +0

    非常感謝!我想到了這一點,但似乎無法做到。很棒。 – Enkay 2009-10-01 21:25:40

    0

    爲什麼不直接重定向到clearsession.php?鏈接= destination.html,從而清除會話,然後再重定向到所需的URL?

    0
    $(document).ready(function() { 
        $("a").click(function(){ 
         var clearSession = "clearsession.php"; 
         $("#content").load(clearSession,""); 
         return false; 
        }); 
    }); 
    

    With 「return false;」,瀏覽器不會跟隨鏈接。