How to get the current URL using AngularJS ?

In this article, we are going to see how to get the current URL with the help of AngularJS. We will be using the $location.absURL() method to get the complete URL of the current page.

Syntax:

$location.absURL()

Example 1: Just use the $location.absURL() method to get the complete URL of the current page.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller",
            function ($scope, $location) {
                $scope.url = '';
                $scope.getUrl = function () {
                    $scope.url = $location.absUrl();
                };
            });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
  
    <p>
        Get current URL in angularJS
    </p>
  
  
    <div ng-app="app">
        <div ng-controller="controller">
  
            <p>Url = {{url}}</p>
  
            <input type="button" 
                value="Click to get URL" 
                ng-click="getUrl()">
        </div>
    </div>
</body>
  
</html>

chevron_right


Output:



Example 2: Similar to the previous example but using the split() method to get the domain name of the URL.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <script src=
    </script>
  
    <script>
        var myApp = angular.module("app", []);
        myApp.controller("controller",
            function ($scope, $location) {
                $scope.url = '';
                $scope.getUrl = function () {
                    // Will change the current url to 
                    // ide.geeksforgeeks.org
                    $scope.url = $location
                        .absUrl().split('/')[2];;
                };
            });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
  
  
    <p>
        Get current URL in angularJS
    </p>
  
  
    <div ng-app="app">
        <div ng-controller="controller">
  
            <p>Url = {{url}}</p>
  
            <input type="button" 
                value="Click to get URL" 
                ng-click="getUrl()">
        </div>
    </div>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.