views:

147

answers:

4

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"&gt;&lt;/script&gt;

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>"
A: 

Try this:

$('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');
});
Sarfraz
No luck with this either the cookie still is not being created :S. I have no idea why?
Thqr
All is working fine bar the creating the cookie.
Thqr
@Ozaki: have you included the cookie plugin in your page? http://plugins.jquery.com/project/Cookie
Sarfraz
Yes. Ive checked that about a million times because thats what i keep thinking but it is definitely there!
Thqr
@Ozaki: make sure that you have included the jquery file first and then cookie plugin.
Sarfraz
yup see edited question copied and pasted straight from my page and both the links follow through correctly.
Thqr
@Ozaki: strange, do you get any error when debugging with firebug consol, etc?
Sarfraz
Not a single error. Hence why I am so confused.
Thqr
Don't know if it makes any difference but the only other script that is run is a getJSON request onload. It is run once and only the once so I don't see how it could interfere.
Thqr
I am however using a cookie elsewhere called "style" to manage alternate stylesheets and it is working perfectly fine. But obviously you can have more than one cookie XD
Thqr
+1  A: 
var value = $(this);

will assign the jQuery object to value. Use

var value = $(this).text();

or

var value = $(this).attr('href');

Kind Regards

--Andy

jAndy
Thanks for the fast response but have tried this still not working :P
Thqr
+2  A: 

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
Zack
This is where I may hit the problem of that my links are generated automatically thus being unable to assign a class. Okay your above is working when i wrap it in its own <script> tags :S. But then again issue of the class name.
Thqr
Well, if they're all to the same URL, you can use your attribute selector in this just as easily as my class selector. Or perhaps a context selector like, `#description > a`. If you show me the markup for the page, I can try to make one for you.
Zack
Pity they are not to the same url. They are part of a different java script library coming from a sever side application. Anyhow I found a sort of solution using your answer will post it below is messy but it works sort of. :P
Thqr
A context selector should still be viable, can you show me where on the page that `<a>` occurs?
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>"Will add it above.
Thqr
Note that the class can change and I didnt want to copy an entire paragraph.
Thqr
I've added some selectors, I can't really tell much from such a small snippet and can't tell if there'd be "collateral damage," but all of those should pick the `<a>` in that snippet.
Zack
A: 

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 ^^

Thqr