This script:
$(function() {
$('a').each(function() {
var href = $(this).attr('href');
if ("a:not(http://)") {
$(this).attr('href', '/' + href);
}
});
});
Add the slash to every link even links with the contain “http://” Not sure why? I do not get any errors?
Any ideas on how I can fix this?
You mixed up two things:
jQuery selectors:
and pure javascript
ifstatements:Both do the same.
Note that they will generate invalid links for other schemes than http (e.g.
/https://example.com/index.html). Depending on how clean the HTML code you’re working with is, you may simply look for the colon to identify absolute links: