Gordian
Gordian

Reputation: 258

how to get domain name from URL string in IE

I have an AngularJs filter returning the domain name of a given URL string.

app.filter('domain', function() {
  return function(input) {
    if (input) {
      // remove www., add http:// in not existed
      input = input.replace(/(www\.)/i, "");
      if (!input.match(/(http\:)|(https\:)/i)) {
        input = 'http://' + input;
      })

      var url = new URL(input);
      return url.hostname;
    }
    return '';
  };
});

the problem is that because I doesn't support URL() method, it doesn't work in IE.

Upvotes: 0

Views: 440

Answers (1)

Reyraa
Reyraa

Reputation: 4274

Yes, according to this document IE doesn't support URL() interface. but let's get out of box! your filter could be written in more short and fast way:

app.filter('domain', function() {
  return function(input) {
    if (input) {
      input = input.replace(/(www\.)/i, "");
      if( !input.replace(/(www\.)/i, "") ) {
        input = 'http://' + input;
      }

      var reg = /:\/\/(.[^/]+)/;
      return input.match(reg)[1];
    }
    return '';
  };
});

Upvotes: 2

Related Questions