Find domain name from an URL using angular JS

 

How we found a domain name i.e www.example.com from and URL (http://www.example.com)

What is URL?

A URL is one type of Uniform Resource Identifier (URI); the generic term for all types of names and addresses that refer to objects on the World Wide Web. The term “Web address” is a synonym for a URL that uses the HTTP or HTTPS protocol.

What is domain name?

The part of a network address which identifies it as belonging to a particular domain.

How to find a domain name from an url using angular js

<!--by www.sibaspage.com-->
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<ul ng-app="myApp" ng-controller="myCtrl">
<li ng-repeat="x in names">
    {{x | u2d}}
</li>
</ul>

<script>
var app = angular.module('myApp', []);
// url to domain
app.filter('u2d', function() {
    return function(url) {
    var domain;
    if (url.indexOf("://") > -1) {
        domain = url.split('/')[2];
    }
    else {
        domain = url.split('/')[0];
    }
    domain = domain.split(':')[0];
    return domain;
    };
});
app.controller('myCtrl', function($scope) {
    $scope.names = [
        'http://www.facebook.com',
        'https://www.youtube.com',
        'ftp://www.face.com',
        'http://www.abcedeft.com/cgi-bin/search.cgi?q=example%20search',
        'http://www.example.com/cgi-bin/search.cgi?q=example%20search&example=test',
        'ftp://ftp.microsoft.com',
        ];
});
</script>

<p>Make your own filters.</p>
<p>This filter, called "u2d", will convert the URL into domain.</p>
</body>
</html>

The result is :

find domain name from url