2013-01-25 122 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)