Javascript <;脚本>;document.write(&&x27;lt;base href=";&&x27;&&x2B;document.location&&x2B;&&x27;/>;)</脚本>;

Javascript <;脚本>;document.write(&&x27;lt;base href=";&&x27;&&x2B;document.location&&x2B;&&x27;/>;)</脚本>;,javascript,html,angularjs,href,Javascript,Html,Angularjs,Href,以下head脚本的目的是什么 <head> <script>document.write('<base href="' + document.location + '" />');</script> ... </head> 元素指定用于文档中包含的所有相对URL的基本URL 根据定义: Document.location只读属性返回位置对象, 其中包含有关文档URL的信息,并提供 用于更改该URL并加载另一个URL的方法 在您的情况下

以下head脚本的目的是什么

<head>
<script>document.write('<base href="' + document.location + '" />');</script>
...
</head>
元素指定用于文档中包含的所有相对URL的基本URL

根据定义:

Document.location只读属性返回位置对象, 其中包含有关文档URL的信息,并提供 用于更改该URL并加载另一个URL的方法


在您的情况下,它将
base href
设置为当前URL。另外,
document.location
相当于
document.location.href

嘿,你能帮我解释一下为什么需要它吗?当前的URL不是默认的吗?为了测试路由选项卡,我下载了这个。我下载了一个zip文件并按原样运行,但它不工作,而是在小提琴上运行。与位置url有关吗?基本url由文档中的所有相关url使用。你能把小提琴连起来吗?
<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>
var app = angular.module('plunker', []);

app.controller('NavCtrl', function($scope, $location) {
    $scope.isActive = function(route) {
        $scope.path = $location.path();
        return $location.path() === route;
    };
});