Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Anchor addClass condition

Iva

Coder
Hello,
I can add a class when the link goes to my own website. This works fine:
JavaScript:
$("a[href^='https://mywebsite.com']").addClass("link-is-internal");

Does anyone here know how to create a condition to add the link-is-external class when the link points outside of my website?

Thank you very match!
Iva
 
Solution
You could use an else if statement instead of else:
JavaScript:
var links = $("a").toArray();

for (link of links) {
  if ($(link).attr("href") != undefined && $(link).attr("href").startsWith("https://mywebsite.com")) {
    $(link).addClass("link-is-internal");
  } else if ($(link).attr("href") != undefined) {
    $(link).addClass("link-is-external");
  }
}
I would use an if else condition to check if the link's href attribute is https://mywebsite.com. If it is, then add the link-is-internal class name, otherwise add the link-is-external class name.
JavaScript:
var links = $("a").toArray();

for (link of links) {
  if ($(link).attr("href") == "https://mywebsite.com") {
    $(link).addClass("link-is-internal");
  } else {
    $(link).addClass("link-is-external");
  }
}
 
I am looking for a solution for any address that starts with https://mywebsite.com.
You can use the startsWith() method.

JavaScript:
var links = $("a").toArray();

for (link of links) {
  if ($(link).attr("href").startsWith("https://mywebsite.com")) {
    $(link).addClass("link-is-internal");
  } else {
    $(link).addClass("link-is-external");
  }
}
 
This solution returns:
Uncaught TypeError: Cannot read properties of undefined (reading 'startsWith') at news.php:1051:27.

My code is:
JavaScript:
var links = $("a").toArray();
for (link of links) {
  if ($(link).attr("href").startsWith("http://d17.luskdesign.cz/")) {
    $(link).addClass("link-is-internal");
  } else {
    $(link).addClass("link-is-external");
  }
}
The actual address of my website is here http://d17.luskdesign.cz/news.php .
 
Add $(link).attr("href") != undefined to the condition to make sure the value of the href attribute is defined.

JavaScript:
var links = $("a").toArray();

for (link of links) {
  if ($(link).attr("href") != undefined && $(link).attr("href").startsWith("https://mywebsite.com")) {
    $(link).addClass("link-is-internal");
  } else {
    $(link).addClass("link-is-external");
  }
}
 
Great! Now it no longer returns an error. Tank you very much!!!

I also have links on the web page without the href attribute, for example in dropdown menu items. This solution adds the link-is-external class to them. How can I skip these links without href attribute in this script please?
 
... i tried this solution but it didn't help.
JavaScript:
const attr = $('a').attr('href');
if ( typeof attr !== 'undefined' && attr !== false ) {
    var links = $("a").toArray();
    for (link of links) {
        if ($(link).attr("href") != undefined && $(link).attr("href").startsWith("https://mywebsite.com")) {
            $(link).addClass("link-is-internal");
        } else {
            $(link).addClass("link-is-external");
        }
    }
}
 
You could use an else if statement instead of else:
JavaScript:
var links = $("a").toArray();

for (link of links) {
  if ($(link).attr("href") != undefined && $(link).attr("href").startsWith("https://mywebsite.com")) {
    $(link).addClass("link-is-internal");
  } else if ($(link).attr("href") != undefined) {
    $(link).addClass("link-is-external");
  }
}
 
Solution
Back
Top Bottom