tags:

views:

83

answers:

6

Hi All, Want to know , How to identify if an <a> has been visited or not in jquery. i.e.

if(`<a> has been visited`)
{
   //execute this code
}
else
{
   //execute that code
}
+2  A: 
henchman
may b I am unable to explain u clearly.
Wondering
have a look at the second section... is that what you want?
henchman
Upvote for second part, that's probably exactly what he wants.
Daniel
Not valid solution imo. What if he visited google.com from outside your page, then arrives to your page, from which he hasn't visited google.com...
chelmertz
A: 

If you mean to check somewhere in the code if the link has been clicked at any given time, just use a variable.

var aClicked = false;
$('identifier to the a').click(function() {aClicked = true});

At the point you want to do the check:

if( aClicked ) {
}
douwe
What would be wrong with my answer? He is clearly not looking for the click event ;)
douwe
not what he's asking for but the answer isn't any more wrong than all the other wrong ones :-)
Pointy
A: 

Are you looking for the click event?

middus
No.say we have hasClass() to identify if class ispresent, likewise do we have any for click?
Wondering
@Wondering: there is not. You can however simulate this by just using addClass/hasClass to your anchors. (or by using attributes)
Daniel
A: 
$('a').click(function(e) {
    e.preventDefault()
    if( !$(this).data('clicked') ) {
       $(this).data('clicked', 1);
       something();
    }
});
meder
A: 

OK....let's do this simple by using class attributes.

In jQuery

$('a').click(function() {
   if (!$(this).hasClass("clicked")) {
     $(this).addClass("clicked");
   }
   //You can toggle by doing this 
   //else  {
   //  $(this).removeClass("clicked");
   //}

   if ($(this).hasClass("clicked")) {
     //Do Stuff....
   }
});

that way, you can iterate through <a> and check if $(this).hasClass("clicked")) like this:

$('a').each(function() {
  if ($(this).hasClass("clicked")) {
     //Do Stuff....
   }
});
The Elite Gentleman
+2  A: 

If I understand correctly you want to detect links (on the page) that user visited previously (either from this or from any other page), right?

I believe there is no 100% way but this hack might help. It goes like this: First you set up your CSS so that there is a distinction between a:link and a:visited which you later use to measure for each link.

For example, you can set-up CSS like this:

a, a:link { margin: 0 }
a:visited { margin-bottom: 1px }

Then if your html is this (clearly stackoverflow.com is visited, and other garbage link is not):

<a href="http://stackoverflow.com"&gt;stackoverflow.com&lt;/a&gt;&lt;br&gt;
<a href="http://fjhgsfljhgsljf"&gt;some unvisited link</a>​

You measure them with this script:

​$('a').each(function () {
    var isClicked = !!parseInt($(this).css('margin-bottom'));
    alert('Is ' + $(this).text() + ' visited: ' + isClicked);
});​​​​​​​​​​​​​​

(!!parseInt('1px') yields true and !!parseInt('0px') yields false)

I chose margin-bottom measurable as it makes no visual distinction in my case. You may chose something else.

Marko Dumic
Test it here: http://jsfiddle.net/LLQKF/
Marko Dumic
!!parseInt(...) makes me shiver. how about if($(this).css('margin-bottom') === '1px'
AndreasKnudsen
@Andreas: No problem, as it is an implementation detail. I'm just illustrating general idea (in somewhat dirty way **:)**).
Marko Dumic
This is the only answer that seems to get the point of the original question. The ability for a page to make this determination is considered a security risk by browser maintainers, as it allows a page to determine that you've been to particular home banking sites, etc. Thus, any "good" solution that seems to work reliably is probably one that will stop working in a browser security update someday.
Pointy