", "text": "<p>What is the purpose of the following script in head?</p>\n\n<pre class="prettyprint"><code>&lt;head&gt;\n&lt;script&gt;document.write('&lt;base href="' + document.location + '" /&gt;');&lt;/script&gt;\n...\n&lt;/head&gt;\n</code></pre>\n\n<p>I somewhat understood that base href is used to set the initial portion of default path. So where does this set the url to?\nLater on I'm using</p>\n\n<pre class="prettyprint"><code>&lt;body ng-app="plunker" ng-controller="NavCtrl"&gt;\n &lt;p&gt;Click one of the following choices.&lt;/p&gt;\n &lt;ul&gt;\n &lt;li ng-class="{active: isActive('/tab1')}"&gt;&lt;a href="#/tab1"&gt;tab 1&lt;/a&gt;&lt;/li&gt;\n &lt;li ng-class="{active: isActive('/tab2')}"&gt;&lt;a href="#/tab2"&gt;tab 2&lt;/a&gt;&lt;/li&gt;\n &lt;/ul&gt;\n &lt;pre&gt;{{ path }}&lt;/pre&gt;\n&lt;/body&gt;\n</code></pre>\n\n<p>with the following controller:</p>\n\n<pre class="prettyprint"><code>var app = angular.module('plunker', []);\n\napp.controller('NavCtrl', function($scope, $location) {\n $scope.isActive = function(route) {\n $scope.path = $location.path();\n return $location.path() === route;\n };\n});\n</code></pre>", "answerCount": 1, "upvoteCount": 773, "dateCreated": "2014-07-10 10:27:21", "dateModified": "2022-10-12 12:17:41", "author": { "type": "Person", "name": "archit" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>The <code>&lt;base&gt;</code> element specifies the base URL to use for all relative URLs contained within the document.</p>\n\n<p>From the Mozilla Developer Network definition:</p>\n\n<blockquote>\n <p>The Document.location read-only property returns a Location object,\n which contains information about the URL of the document and provides\n methods for changing that URL and load another URL.</p>\n</blockquote>\n\n<p>In your case it sets the <code>base href</code> to the current URL. Additionally, <code>document.location</code> is equivalent to <code>document.location.href</code>.</p>", "upvoteCount": 113, "url": "https://exchangetuts.com/scriptdocumentwritebase-href-documentlocation-script-1641308824510167#answer-1658607737890957", "dateCreated": "2022-10-10 23:17:41", "dateModified": "2022-10-12 12:17:41", "author": { "type": "Person", "name": "Luke Peterson" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    };
});
like image 773
archit Avatar asked Jul 10 '14 10:07

archit


1 Answers

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.

like image 113
Luke Peterson Avatar answered Oct 12 '22 12:10

Luke Peterson