Pour _link () url with href value using jquery

How can I get the value of the href attribute in a link to populate the value of the _link () url with jquery?

This is the normal code:

<a href="http://www.mydomain.com/gallery/" onclick="_gaq.push(['_link', 'http://www.mydomain.com/gallery']); return false;">Gallery</a>

There are many links that need to be updated in this way.

+3
source share
2 answers

You can create a class for the links for which you want to run _gaq.push, and then use jQuery live () to bind the click for them.

Give your links a class like gaq:

<a class="gaq" href="http://www.mydomain.com/gallery/">Gallery</a>

And use jQuery live () to give all links with this class the ability to trigger the push function on click.

$('.gaq').live('click', function() {
    _gaq.push(['_link', $(this).attr("href")]);
    return false;
});
+4

, - <a> , .

$('.aClass').attr('href') //gets the value of the href
$('.aClass').attr('href', 'stackoverflow.com') //sets the value of the href

$('.aClass').click(function(){
    _gaq.push(['_link', $(this).attr('href')]); 
    return false;
});

, . attr () .

+2

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


All Articles