vietean
vietean

Reputation: 3033

In best case how can I check a link belongs to *.domain.com?

How can I check a link belongs to a domain *.domain.com using javascript?

I mean that I ignore subdomain.

Upvotes: 0

Views: 67

Answers (1)

kubetz
kubetz

Reputation: 8556

If we are talking about links on the page (<a> elements), then try this:

// regular expression to check for domain.com
// won't match tricky ones like domain.company.com nodomain.com :)
var re = /(^|\.)domain\.com(\/|$)/i; 

// loop through all the links
$("a").each(function() {
  // test if href attribute belongs to domain.com
  if(re.test($(this).attr('href'))) {
    // do what you want with links that belong to domain.com
    $(this).addClass("domaincom");
  }
});

HERE is the code.

Upvotes: 3

Related Questions