简体   繁体   English

脚本src和Link rel中的相对路径始终解析为根URL

[英]Relative path in Script src and Link rel keeps resolving to root URL

I have a document located at http://localhost:8081/develop . 我有一个位于http://localhost:8081/develop的文档。

The index.html , located at http://localhost:8081/develop/index.html contains the following: index.html ,位于http://localhost:8081/develop/index.html包含以下内容:

<html>
    <head>
        ...
        <link rel="stylesheet" href="./styles.min.css">
        <script src="./bower_components/jquery/dist/jquery.js"></script>
        <script src="./bower_components/moment/min/moment-with-locales.js"></script>
        <script src="./bower_components/elessar/dist/elessar.js"></script>
        <script src="./bower_components/EventSource/eventsource.js"></script>
        ...
    </head>
    <body>
        ...
    </body>
</html>

ALL of the files starts with a ./ , telling the browser that it should search them in the current directory, for example: <script src="./bower_components/jquery/dist/jquery.js"></script> should resolve into http://localhost:8081/develop/bower_components/jquery/dist/jquery.js . 所有文件都以./开头,告诉浏览器应该在当前目录中搜索它们,例如: <script src="./bower_components/jquery/dist/jquery.js"></script>应该可以解析进入http://localhost:8081/develop/bower_components/jquery/dist/jquery.js That's obvious. 很明显

But it doesn't. 但事实并非如此。 Instead, they are being resolved into http://localhost:8081/bower_components/jquery/dist/jquery.js , throwing tons of errors in the console. 相反,它们被解析为http://localhost:8081/bower_components/jquery/dist/jquery.js ,在控制台中http://localhost:8081/bower_components/jquery/dist/jquery.js大量错误。

在此处输入图片说明

The same thing will happen if instead of using ./ in the beginning of the files, I'd remove the first slash: <script src="bower_components/jquery/dist/jquery.js"></script> also resolves into http://localhost:8081/bower_components/jquery/dist/jquery.js . 如果在文件的开头而不是使用./ ,则将发生相同的情况,而是删除第一个斜杠: <script src="bower_components/jquery/dist/jquery.js"></script>也会解析为http://localhost:8081/bower_components/jquery/dist/jquery.js

It feels like a very basic question but I really need that those files have relative paths and that those paths works as expected. 这似乎是一个非常基本的问题,但我确实需要这些文件具有相对路径,并且这些路径能够按预期工作。 So why aren't them? 那为什么不呢?

原因可以在开头部分标记<base href="/">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM