根据点击的链接的名称设置Cookie [英] Setting a cookie based on the name of the link that is clicked

查看:97
本文介绍了根据点击的链接的名称设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR 点击链接时,我要分配一个包含乐器名称的Cookie,并点击链接上的文字值。



使用Jquery.1.4.2.min.js,Jquery.cookie.1.0.js








仪器名称





$ b

code>< script type =text / javascriptsrc =scripts / jquery-1.4.2-min.js>< / script>
< script type =text / javascriptsrc =scripts / jquery.cookie-1.0.js>< / script>

Link1:

 code>< a href =page.html> link1< / a> 

Link2:

 code>< a href =page.html> link2< / a> 

脚本:

  $('a [href = page.html]')。click(function()
{
var name ='instrument';
var value = $ ).text();
$ .cookie(name,value,{expires:365});
});

当我点击链接,它只是加载链接,没有设置cookie。
用firebug,firecookie,firequery调试。没有找到仪器的cookie或任何沿线。 Onload我将点击



< a href =page.html> projects< / a> 而不是



$。cookie(name,value,{expires:365});



即使使用以下内容:(感谢 Sarfraz < a>)

  $('a [href =page.html]')。 b $ b {
//防止默认动作
e.preventDefault();
var name ='instrument';
//获取链接文本
var value = $(this).text();
//设置cookie
$ .cookie(name,value,{expires:365});
//现在转到链接的位置
document.location.href = $(this).attr('href');
});

仍然不会创建cookie。



如果我在 $。cookie〜上添加一个断点,即使点击其中一个链接,也不会碰到
断点和文档.location.href〜。



对于Zack:



Pre Render -
< script type =text / javascriptsrc =http:// localhost / tree>< / script>



After Render -

 < div class =dtree> 
< div class =dTreeNode>< img alt =src =Images / base.pngid =id0>< a onclick =javascript:ds(0);href = homeclass =nodeid =sd0>首页< / a>< / div>


解决方案

这是hackish,但它适用于我。 / p>

  $(document).ready(function(){
$('a.cookier')。 (){
//杀死链接的链接特性
_href = $(this).attr('href');
$(this).attr(rel,_href) .attr(href,javascript:void(0););
});
$('a.cookier')。 ='instrument';
var value = $(this).text();
$ .cookie(name,value,{expires:365});
//转到
document.location.href = $(this).attr('rel');
});
});

我的标记如下所示:

 < a class =cookierhref =hrrrngh.html> Shazam!< / a> 

我使用类似的方法取消链接我使用AJAX的页面上的链接,但必须降级优雅。



注意:您可能需要选择一个比cookier更高级的类。





您可以选择那些< a> 中的任何一个, >不选择页面上的其他内容。

  .dtree img + a 
.dTreeNode > a
.dTreeNode a.node
.dTreeNode a [onclick = | javascript] .node


TLDR When clicking on a link I want to assign a cookie with a name of instrument and a value of the text on the link clicked.

Using Jquery.1.4.2.min.js, Jquery.cookie.1.0.js


I am trying to create a cookie when a link is clicked (will always link to "page.html").

name of instrument

value of the TEXT

So far I am trying to use:

<script type="text/javascript" src="scripts/jquery-1.4.2-min.js"></script> 
<script type="text/javascript" src="scripts/jquery.cookie-1.0.js"></script>  

Link1:

<a href="page.html">link1</a>

Link2:

<a href="page.html">link2</a>

Script:

$('a[href=page.html]').click(function()
{
var name = 'instrument';
var value = $(this).text();
$.cookie(name, value, { expires: 365 });
});

When I click the link it just loads the link and no cookie is set. Debugging with firebug, firecookie, firequery. No cookie for instrument or anything along the lines is found. Onload I'll hit the

"<a href="page.html">projects</a>" but not the

"$.cookie(name, value, { expires: 365 });" at all.

Even using the below: (Thanks to Sarfraz)

$('a[href="page.html"]').click(function(e)
{
 // prevent default action
 e.preventDefault();
 var name = 'instrument';
 // get link text
 var value = $(this).text();
 // set cookie
 $.cookie(name, value, { expires: 365 });
 // now go to link's location
 document.location.href = $(this).attr('href');
});

Still will not create the cookie.

If I add a breakpoint onto $.cookie~ even on clicking one of the links it does not hit the breakpoint nor the document.location.href~.

For Zack:

Pre Render - <script type="text/javascript" src="http://localhost/tree"></script>

After Render -

"<div class="dtree">
<div class="dTreeNode"><img alt="" src="Images/base.png" id="id0"><a onclick="javascript: d.s(0);" href="home" class="node" id="sd0">Home</a></div>"

解决方案

This is hackish, but it works for me.

$(document).ready( function() {
    $('a.cookier').each( function() {
        // Kill the link's link feature.
        _href = $(this).attr('href');
        $(this).attr("rel", _href).attr("href", "javascript:void(0);");
    });
    $('a.cookier').click(function() {
        var name = 'instrument';
        var value = $(this).text();
        $.cookie(name, value, { expires: 365 });
        // Go places
        document.location.href = $(this).attr('rel');
    });
});

My markup looks like this:

<a class="cookier" href="hrrrngh.html">Shazam!</a>

I use a similar method for "un-linking" my links on pages that use AJAX but must degrade gracefully.

Note: You'll probably want to pick a classier class than "cookier."

Edit:

You could select those <a>'s in there with any of these, pick whichever doesn't select other stuff on the page.

.dtree img + a
.dTreeNode > a
.dTreeNode a.node
.dTreeNode a[onclick=|javascript].node

这篇关于根据点击的链接的名称设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆