Forum Moderators: open
for(i = 0; i < document.links.length; i++){
if(document.links[i].href.indexOf(window.location.host+window.location.pathname+'#') !== -1){...}} #
#anchor
http://www.mysite.tld/file1.html
http://www.mysite.tld/file1.html#
http://www.mysite.tld/file1.html#anchor file2.html
http://www.mysite.tld/file2.html
http://www.othersite.tld/file3.html
file2.html#anchor
http://www.mysite.tld/file2.html#anchor
http://www.othersite.tld/file3.html#anchor document.links[i].href.indexOf(window.location.host+window.location.pathname+'#') !== -1
But not:
file2.html
h t t p://www.mysite.tld/file2.html
h t t p://www.othersite.tld/file3.html
file2.html#anchor
h t t p://www.mysite.tld/file2.html#anchor
h t t p://www.othersite.tld/file3.html#anchor
http://www.mysite.tld/file1.html http://www.mysite.tld/file1.html
http://www.mysite.tld/file1.html?id=7 function regQuote() {
return arguments[0].replace(/([\-\.])/, function(m) {
return '\\' + m[1];
});
}
function checkLink() {
var href = arguments[0];
var regexp, check;
if(href.match(/^\#/)) {
// Starts with a hash. Just an anchor on this page
return true;
}
if(href.match(/^https?:\/\//)) {
// If it starts with http(s)://, we need to check the domain
regexp = new RegExp('^https?:\\/\\/' + regQuote(window.location.host) + '($|/|\\#|\\?)');
if(!href.match(regexp)) {
return false;
}
}
if(href.match(/^\//)) {
// Starts with a slash. Need to validate against entire path
check = window.location.pathname
} else {
// Does not start with a slash. Just want the end segment
check = window.location.pathname.split('/').pop();
}
regexp = new RegExp('^' + regQuote(check));
if(!href.match(regexp)) {
return false;
}
return true;
}
/* USAGE:
if(checkLink(document.links[i].href) === true) {
// Link to this page
}
*/
After some tests, it appears that my_a_dom_node.href always return the full-qualified URI, including the [domaine.name,...] which should be okay for what I want.
[edited by: whoisgregg at 6:45 pm (utc) on Jul 29, 2013]
[edit reason] rv link, please see sticky mail :) [/edit]