Yesterday I asked the similar question, so I get to know how to redirects all the external links to my home page.
// when the document is ready
$(document).ready(function() {
// iterate over all the anchor tags
$("a").each(function() {
// if the current link's href doesn't already contain 'www.kownleg.com'
if (this.href.indexOf('www.kownleg.com') === -1) {
// change current link to home page
this.href = 'http://www.kownleg.com';
}
});
});
But now I want to exclude all the links which I don't want to redirect to my home page, like facebook.com & twitter.com, I tried to make another condition for the links which I don't want to redirect, but its not working. Then I tried to change indexOf()
with text()
but still not working.
Here is two codings which I tried, but I failed
// when the document is ready
$(document).ready(function() {
// iterate over all the anchor tags
$("a").each(function() {
// if the current link's href doesn't already contain 'www.kownleg.com'
if (this.href.indexOf('www.kownleg.com') === -1) {
// change current link to home page
this.href = 'http://www.kownleg.com';
}
if (this.href.indexOf('www.facebook.com') === -1) {
// change current link to home page
this.href = 'http://www.facebook.com';
}
if (this.href.indexOf('www.twitter.com') === -1) {
// change current link to home page
this.href = 'http://www.twitter.com';
}
});
});
Another One:
// when the document is ready
$(document).ready(function() {
// iterate over all the anchor tags
$("a").each(function() {
// if the current link's href doesn't already contain 'www.kownleg.com'
if (this.href.indexOf('www.kownleg.com') === -1) {
// change current link to home page
this.href = 'http://www.kownleg.com';
}
if (this.href.text('www.facebook.com') === -1) {
// change current link to home page
this.href = 'http://www.facebook.com';
}
if (this.href.text('www.twitter.com') === -1) {
// change current link to home page
this.href = 'http://www.twitter.com';
}
});
});
And all the similar possibilities... but still not work for me.