views:

72

answers:

5

Hi,

I have a problem and I want to resolve it with jQuery.

I have a some Links on the page, and want to get the "text" from the link what was clicked. But I need this text to use on another page. Because when I click this anchor I leave this page , and I go to another one, and I need to use this "Get link text" on the second page.

I Hope it is possible to use browser Cookies with jquery helps ..

Can you help me, if it is possible!!

Thanks !

A: 

Read here how to set and read cookies -- then you can make the link set the cookie using onclick="" and at the end of the onclick return true; to make it link to where it was supposed to right after it finishes. Basically,

<a href="http://google.com" onclick="setCookie('link_title','Go to Google');return true;">
Go to Google
</a>
henasraf
+1  A: 

How about adding the text to request parameters and using it on server side?

Konrad Garus
+5  A: 
$("a").click(function(event){
    myTarget = $(this).attr('href')+"?txt="+$(this).text();
    window.location.href = myTarget;
});

This will pass the text of the link to the next page as a query string parameter called "txt".

Does that help?

inkedmn
+1 for not abusing cookies. You could also potentially use fragment identifiers (http://www.w3.org/TR/html401/intro/intro.html#fragment-uri) to pass the text.
Chetan Sastry
I agree with this, but having a GET parameter appended to the URL such as `www.example.com?thelinktext=Thisisthetextofthelink` on the requested page would look a bit unwieldy (IMO).
karim79
if you were *really* serious about avoiding the use of cookies here, you could also post the value using a form - but even suggesting that made my soul a little blacker.
inkedmn
A: 

Try the cookie plugin for jquery: http://plugins.jquery.com/project/cookie

chris166
A: 

Using the Cookie plugin:

$(document).ready(function() {
    $('a.something').click(function() {
        $.cookie('my_cookie', $(this).text());
    });
});


// on your second page
$(document).ready(function() {
    $('a.something').text($.cookie('my_cookie'));
});
karim79

related questions