Add Current Page URL Before Anchor Link Using JQuery
I am stuck utilizing a platform (Nationbuilder) that automatically adds the base tag in the header. It didn't until recently and now the dozens of pages with anchor links end up se
Solution 1:
You can use the CSS attribute starts with selector:
Instead of selecting "a"
, select 'a[href^="#"]'
meaning "a tags with an href attribute starting with #
.
So something like:
$('a[href^="#"]').each(function(i,el){
el.href = "http://www.example.com/pageslug" + el.hash;
});
Solution 2:
One addition to what Benjamin said, you'll need to capture the current href for each of the anchors, which means you need a loop. So
$('a[href^="#"]').each(function(index, element){
var $ele = $(element),
oldHref = $ele.attr('href');
$ele.attr('href', '//example.com/page'+ oldHref);
});
Solution 3:
Something like this?
$('a').attr('href', window.location.pathname + this.attr('href'))
Post a Comment for "Add Current Page URL Before Anchor Link Using JQuery"