Setting a cookie based on the name of the link that is clicked

后端 未结 4 573
一个人的身影
一个人的身影 2021-01-13 16:28

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

4条回答
  •  -上瘾入骨i
    2021-01-13 16:42

    Using the answer from Zack above I changed the element tag. The only issue is it will set the cookie for whichever link is followed wether it be for the link I want or not but it sets the cookie and works for where it is needed. IF there is a cleaner approach it would be appreciated.

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

    Massive thanks to Sarfraz and Zack for all the help at least I have something that works for now ^^

提交回复
热议问题