I've got the following code to track pageviews for external links matching a particular URL.
$("a").each(function(i){
if (
$(this).attr('href') == "http://example.com/external/link/" ||
$(this).attr('href') == "http://example.com/external/link"
) {
$(this).click(function(){
_gaq.push(['_trackPageview', '/external/pagename']);
});
}
});
This code works, but it's extremely inefficient for pages with a lot of links. Is there a way to use a selector to select all the anchors with matching hrefs instead of scanning through all the links on the page?
If you want to get any element that has part of a URL in their href attribute you could use: $( 'a[href*="google.com"]' ); This will select all elements with a href that contains google.com, for example: http://google.com.
To find the anchor elements in a particular web page, we need to open the source of web page by using the browser. After that, you can click ctrl+u. Then, you can copy the source code in the text and also click ctrl+h. It is a simple way to find the anchor text.
jQuery [attribute$=value] Selector The [attribute$=value] selector selects each element with a specific attribute, with a value ending in a specific string.
getElementsByTagName('a'), for example, selects all anchor tags (links) on a page); and some browsers support methods for selecting all elements with a particular class or using a CSS selector to select page elements.
You can use the Attribute Starts With Selector
$('a[href^="http://example.com/external/link"]').click(function() {
_gaq.push(['_trackPageview', '/external/pagename']);
});
Yes
$('a[href^="http://example.com/external/link"]').click(function() {});
Using the attribute selector you can look for a particular href
. Instead of the normal href=
you might expect, I have used href^=
which matches any element which starts with the specified string.
Also you do not need to use each
to bind to the click event of all the anchor tags you will select. click()
will automatically do this for you.
$("a[href^="http://example.com/external/link"]").click(function(){
_gaq.push(['_trackPageview', '/external/pagename']);
});
In jQuery getting all hrefs would be like:
var href = 'http://www.google.com';
var elements = $('a[href=' + href + ']');
alert("Found: " + elements.length);
You can use attribute ends with selector as well if you need to get elements with some specific ending attribute. Something like this:
$('a[href$=your text]') == "your text"
Hope this helps someone.
Another Simple way is:
$('a[href="MY_ANCHOR_TEXT_HERE"]');
Gives the list of all the anchor tags in the current page. To get the anchor tags in a div ID you can use:
$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]')
To get the size use:
$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]').size()
$('a[href^="http://example.com/external/link"]').click( function(e){
// you do want to track which link was clicked, yes?
_gaq.push(['_trackPageview', $(this).attr('href') ]);
// suppress default click or you'll leave the page immediately:
e.preventDefault();
do_other_stuff_presumably_with_gaq();
});
Here you can the code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").each(function(){
if($(this).attr("href") == "http://example.com/external/link/")
{
$(this).hide();
}
});
});
</script>
<a href="http://example.com/external/link/">a website link</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With