Record a hyperlink to my site

I have a website where I allow other developers to post content. My goal is to record clicks on each hyperlink (even on content posted by other developers) that exists on the page.

My initial approach was as follows:

$('a').click(function(event) { //do my logging return true; } ); 

Now that the above approach, I am facing the following problems:

  • Developers may have images inside the anchor link, so the goal of the goals is to image, not href
  • Many developers have their own way of handling href clicks using the onclick event, rather than just href = '' attr
  • Some developers add their own attr to the tag and have custom click processing features.

so basically the problem is that there are so many anchor tags available, and clicks to log into the system are not so simple.
Many cases allowed me to register the data that I wanted, but in several cases I violated the code a lot.

My goal of posting on this forum is:

  • to discuss which approach is suitable for clicks on hyperlinks in a dynamic environment.
  • is there a plugin that allows you to use such functions.

I know that facebook and google have this, but they have a totol control on what is hosted in their environment.

Any help is greatly appreciated.

+4
source share
2 answers

Adding a click handler to each link is not a good idea. You should use event delegation (which will only attach the event handler one in the root directory of the document):

 $(document).delegate('a', 'click', function(event) { // logging }); 

Update (12/17/2011):

Starting with jQuery 1.7, you can use .on() [docs] :

 $(document).on('click', 'a', function(event) { // logging }); 

Regarding your concerns:

Developers can have images inside the anchor, so the goal of the goals is the image, not the href

Events bubble up until distribution is canceled. It depends on what you want to register. With delegate event.target property will point to the image, but this (inside the handler) will point to the a element.
Therefore, there should not be any problems (example: http://jsfiddle.net/cR4DE/ ).

But it also means that you will miss the clicks if the developers cancel the distribution.

(Side note: you can solve this by allowing the event handler in the step, but IE does not support this (hence jQuery does not).)

Many developers have their own way of handling href clicks using the onclick event, rather than just href = '' attr

This will not affect existing event handlers.

Some developers add their own attr to the tag and have custom click processing features.

Not sure what you mean here.


It also depends on how other content is included. For instance. the above code will not track clicks in the iframe.

+8
source

In your registration code, you should check for bad deeds and take appropriate action.

For example, in your first case, I get the image and go home until I find the a tag and register href from there.

There will be times when you cannot complete the registration, but if they are small compared to the cases you can do, you will be fine :).

+1
source

Source: https://habr.com/ru/post/1335093/