<script>document.write('<base href="' + document.location + '" />');</script>

angularjs, href, html, javascript

Solution

The `<base>` element specifies the base URL to use for all relative URLs contained within the document.

From the Mozilla Developer Network definition:

The Document.location read-only property returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and load another URL.

In your case it sets the `base href` to the current URL. Additionally, `document.location` is equivalent to `document.location.href`.

Problem

What is the purpose of the following script in head? ``` <head> <script>document.write('<base href="' + document.location + '" />');</script> ... </head> ``` I somewhat understood that base href is used to set the initial portion of default path. So where does this set the url to? Later on I'm using ``` <body ng-app="plunker" ng-controller="NavCtrl"> <p>Click one of the following choices.</p> <ul> <li ng-class="{active: isActive('/tab1')}"><a href="#/tab1">tab 1</a></li> <li ng-class="{active: isActive('/tab2')}"><a href="#/tab2">tab 2</a></li> </ul> <pre>{{ path }}</pre> </body> ``` with the following controller: ``` var app = angular.module('plunker', []); app.controller('NavCtrl', function($scope, $location) { $scope.isActive = function(route) { $scope.path = $location.path(); return $location.path() === route; }; }); ```

Original source