Storing the value in a href tag

how can I stored the value within an a href tag for example my php page does this

echo "<li><a href=\\"JavaScript:article('$id')\\">".$row['title']."</a> </li>";

which prints this code on my ajax page

<a href="JavaScript:article('40')">What is the difference between Archiving and Trashing an Article? </a>

. I want to save the value within href so that I can use it later. Please note its ajax so location.href doesn’t work.

Thanks

if I can store the entire link tag that wouldn’t be an issue either. In fact that may be preferred.

Is that link intended to be saved as a bookmarklet? If not then get the JavaScript out of there and into a click event where it belongs… The href should be used for where you want people without JavaScript to go when they click the link.

Once you get the script out of the HTML and into a separate JavaScript file you will be able to more easily change it to work the way you want.

Yes i want to use that link as a bookmark but when u click on it because it is ajax i dont see that in the address bar so i need a next way to save that value

Virtually all web browsers prevent links from automatically saving as a bookmark, so the standard practice now is to instruct the user to drag the link to their bookmarks/favorites location.

For an example, see http://delicious.com/help/bookmarklets

Ok well I am not really trying to save this to the browser per say but to localStorage so that when the user visits this page again on there mobile phone the links deh favor can be accessed in its own bookmark section within the web app. Is that still not a preferred practice as I see no other way to save the users favorite articles. The drag and drop will not be favorable for all phones hence why I am asking this.

Local storage can be used, but it’s available only with HTML5. What you could do is to check if local storage is available, and if it’s not you could use cookies instead to store the info.

Since your links contain javascript, you should serialize the data first, before saving it to either local storage or a cookie.

I dont know what serialize means. Also even if i used cookies I still don’t know how to save the link itself as it doesn’t appear in the address bar since its ajax am using so i dont know how to access the link after i click on it

There might be quite a lot for you to learn here, but we’ll give it a go.

To serialize, means to create a text-string version of the code. That might not strictly be necessary, but it can be a useful safety measure.
The JSON library provides a nice and useful way to serialize data.

What you can do us to attach an onclick event to the section in which the link is contained. That way any click event will bubble up to where the onclick event is captured, so that you can then work with the link that was clicked.

ok will read up on the JSON it looks a lil more complicated than i thought however on the onclick what function can I use to capture the <a> tag that the onclick came from? As i said I think once I can get the link I would be much better of in storing that data

That all depends on how your page is structured.

As a simple example, imagine that we have a content section in to which is placed the links.


<div id="content">
    <a href="#part1">Part 1</a>
    <a href="#part2">Part 2</a>
</div>

With JavaScript you can monitor all onclick events that occur within the content element.


document.getElementById('content').onclick = clickHandlerForLinks;

and that clickHandlerForLinks function can obtain the link that was clicked on, with which you can do what you like with it.


function clickHandlerForLinks(evt) {
    evt = evt || window.event;
    var targ = evt.target || evt.srcElement;
    if (targ.nodeType === 3) {
        targ = targ.parentNode;
    }
    if (targ.nodeType === 1 && targ.nodeName === 'A') {
       // we have a link, do what you will with it.
       doSomethingWith(targ);
    }
    return false;
}

The evt line get the event in a cross-browser compatible way, since Internet Explorer likes to do things differently from most other web browsers.

The targ line obtains information from the event about what was clicked. Once again, Internet Explorer is the odd one out using srcElement whereas most other web browsers use target instead.

Checking that the nodeType equals 3, is to deal with an Opera bug of setting the target to a text node, so taht we can get the target back to an element node instead.

The if statement checks that we have an element node (nodeType of 1) and that the element node has a nodeName of “A”.

ok that was a bit confusing am a beginner so bear with me I am to assume that the tag will be stored in the targ variable correct also m going to attach my code to this post to see if it makes sense to you. So my new_articles.html calls new_articles.php toobtain the list of articles new_articles.php echos the urls with <a> tags and all. When the user now click on the link it calls article_desc.php to display the article. so basically i want to save the url that calls article_desc.php when a user clicks a button to make it a favorite.
Sorry if the code is scrappy but I haven’t gotten around to cleaning it up yet

I will certainly help where I can to help you understand what you need to know in order to achieve what you want to do, but I’m afraid that I won’t do the actual work for you.

I have to head off now, but if I have time later on I’ll take a look over your code and see if there’s anything helpful that I can come up with.

No no am not trying to do that am trying to see how the code you send will fit in in my instance your code you put earlier made reference to some html code and javascript but I don’t understand it and where it applies. So i cannot begin to even tweek it to suit my needs. The reason i sent the code was to speak about it in my case not to make you my resident coder.

The first thing that I see is that you’re using Joomla for your framework. Very few of us here know much about Joomla, so I would get on to the Joomla forums to find out if Joomla contains anything that can help you to achieve what you require in a much easier way.

Failing that, we can fall back on to some old-school JavaScript programming.

While I am using Joomla it is only the db so the Joomla framework will not help in this instance. Everything i built was from scratch. I tried the code you sent before and it pulls the link fine however I get an error when trying to use the link the Error is

Forbidden You don’t have permission to access /Weblog 5/JavaScript:article(‘39’) on this server.
Which is very strange. Now going to read up on JSON