2013-01-25 52 views
1

這是顯示URL和刪除圖像以刪除cookie的代碼。添加和顯示功能正在工作,但如何刪除?如何使用jQuery從Cookie中刪除特定的Cookie

function backLinks(){ 
    var pathname = window.location; 
    var patientName = document.getElementById("general:patientDetailName").value; 
    var cookieTimeVal = jQuery.cookie('PCC_Back_Button'); 
    if(cookieTimeVal== null){ 
     cookieTimeVal =""; 
    } 
    // for writing Cookie 
    var stringCookie = "<span class='backLinkText1'><img src='../images/deleteImg.png' alt='' class='backLinkDeleteButton' onClick='deleteBackLink()'/></span><a class='backLinkText' href=\""+pathname+"\"> Patient History For \""+patientName+"\"</a>"+cookieTimeVal; 
    jQuery.cookie('PCC_Back_Button', stringCookie , { expires: 1 }); 

    // read Cookie and set in HTML 
    jQuery('#backButtonSpan').append(
     jQuery('<div>').attr({style:'padding-top:-10px;' }).append(cookieTimeVal) 
    ); 
} 

**

function deleteBackLink(val){ 
     jQuery.cookie(val, null); 
    } 

**

如何創建刪除功能,我將傳遞給它什麼參數?

+0

請問你有什麼不行?我建議雞肉。 –

回答

2

得到了在這一個正確的答案...

我將代替餅乾和刪除內HTML

function backLinks(stringValueAndName, patientName, patientDOB){ 
       var pathname = window.location; 
       var cookieTimeVal = jQuery.cookie('PCC_Back_Button'); 
       if(cookieTimeVal== null){ 
        cookieTimeVal =""; 
       } 

       var time = new Date(); 
       var spanId = time.getTime(); 

       // for wright in Cookie 
       var stringCookie = "<span id ="+spanId+"> <img src='../images/deleteImg.png' class='backLinkDeleteButton' onClick='deleteBackLink("+spanId+")'/><a class='backLinkText' href=\""+pathname+"\">"+stringValueAndName +patientName+' ('+patientDOB +')'+"\</a></span>"+cookieTimeVal; 
       jQuery.cookie('PCC_Back_Button', stringCookie , { expires: 1 }); 
       // read Cookie and set in HTML 
       jQuery('#backButtonSpan').append(
         jQuery('<div>').attr({style:'padding-top:-10px;' }).append(cookieTimeVal) 
        ); 
      } 
    function deleteBackLink(val){ 
     jQuery('#'+val).remove(); 
     var stringCookie = jQuery('#backButtonSpan div').html(); 
     jQuery.cookie('PCC_Back_Button', stringCookie , { expires: 1 }); 
    } 
0

要刪除使用jQuery一個cookie的值設置爲null:

jQuery.cookie("name", null); 

所以你的函數將工作 - 只是通過cookie名稱作爲參數:

deleteBackLink("name"); 
+0

這是輸出患者病史對於「krishan」,患者病史對於「krishan」,主頁和我想只刪除主頁我應該傳遞什麼參數? –

+0

你能看到正在設置的cookie嗎?如果cookie名稱是'krishan',只需使用jQuery.cookie(「krishan」,null);或deleteBackLink(「krishan」); –

+0

請仔細檢查問題 –

0

它不。 Cookie是一個cookie。

The closest it comes is the HTTP Only flag, which allows a cookie to be hidden from JavaScript(mean client side)。 (這爲XSS cookie盜竊提供了一些防禦)。

一個cookie是一個cookie。 (Again, client side code can't touch an HTTP only cookie)