Suppose we have a page with the following structure:
<li id="A">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>
<li id="B">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>
<li id="C">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>
Is it possible, with PHP or JS, to grab the list id (A,B,C) and append it along with the referring URL when a person clicks on any one of the three links?
UPDATE
In light of your revelation that you have no access to the original page, you've got no chance of finding out anything about the specific link that was clicked to arrive at your page. You can get the referrer with document.referrer
.
Previous answer for posterity
The following function will capture a click on any link within the container element with the specified id and add a query string containing the id of the container element and the URL of the current page to the URL:
function modifyLink(containerId) {
var el = document.getElementById(containerId);
el.onclick = function(evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
if (target.tagName == "A") {
window.location.href = target.href + "?id=" + encodeURIComponent(containerId) +
"&referrer=" + encodeURIComponent(window.location.href);
return false; // Cancel the default link behaviour
}
};
}
modifyLink("A");
modifyLink("B");
modifyLink("C");
We have established (via comments on another answer) that the questioner owns the site being linked to, not the one containing the links.
Therefore, there is nothing you can do to manipulate the links in the code he's quoted, which means that neither Javascript nor PHP, nor indeed any other language will help you.
So the obvious answer is no: As the owner of example.com, you can't tell which of those three links was the one that was clicked on to arrive at your site. If the links were on separate pages, you would be able to tell from the referral data, but for links on the same page, HTTP simply doesn't supply that information to you, so you can't tell. You'd only be able to tell if the links were pointing to different URLs (maybe with a query parameter), but that would need the links to be modified and you cannot modify the links yourself.
About the only solution I can offer you is the old-fashioned answer: contact the owner of the site containing the links, and ask him to modify them so that they are unique.