views:

45

answers:

2

Suppose we have a page with the following structure:

<li id="A">
<span class="some class">some content
<a href="http://www.example.com"&gt;http://www.example.com&lt;/a&gt;
</span>
</li>

<li id="B">
<span class="some class">some content
<a href="http://www.example.com"&gt;http://www.example.com&lt;/a&gt;
</span>
</li>

<li id="C">
<span class="some class">some content
<a href="http://www.example.com"&gt;http://www.example.com&lt;/a&gt;
</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?

  • Need to know specifically which one of the three list tags the click originated from
  • I don't have write access to the originating page
A: 

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");
Tim Down
Yes, this does answer the question posed. My mistake for not letting in on the fact that I don't have write access to the originating page. Only read access. I wont be able to include any Javascript on the originating page. Is it still possible?
egfx
@egfx: I presume that means you're the owner of the site being linked to, rather than the one that contains the code that you've quoted? That's a fairly fundamental point to how the question would be answered! You should probably amend the question to include that information.
Spudley
@Spudley Yes, Thanks I did include that snippet of info already. I overlooked that fact when posing the question. Thats exactly right. I own the site being linked to but do not own the site with the HTML structure that is included in the example.
egfx
Right, that changes things. I've updated my answer.
Tim Down
A: 

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.

Spudley
Yeah, umm. Considering the site is Twitter, contacting them to change their layout to accommodate my little use case may be a hurdle ;). Thanks though for all your input. I'm marking the above answer with the code as solved.
egfx