Html 为什么开发人员会在每个相对路径的开头放置一个正斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7613274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:51:40  来源:igfitidea点击:

Why would a developer place a forward slash at the start of each relative path?

htmlhyperlinkrelative-path

提问by stefmikhail

I am examining some code for a friend, and have found that the developer who built his site began each and every relative src, href, and includewith a forward slash /.

我正在为一位朋友检查一些代码,发现构建他的网站的开发人员以每个亲戚src, href, 和include正斜杠开头/

For example:

例如:

src="/assets/js/jquery.js"

I have never seen this before. So my question is, why would a developer place a forward slash /at the start of a relative path?

我以前从未见过这个。所以我的问题是,为什么开发人员会/在相对路径的开头放置一个正斜杠?

回答by Oded

It's done in order to root the path (making it an absolute path).

这样做是为了根路径(使其成为绝对路径)。

It ensures that the path is not relative but read from the root of the site.

它确保路径不是相对的,而是从站点的根目录读取的。

This allows one to move a file around and not have to change the links to the different resources.

这允许人们移动文件而不必更改指向不同资源的链接。

Using your example:

使用您的示例:

src="/assets/js/jquery.js"

If the referencing file is in /pages/admin/main.html(for example) using relative paths you would use:

如果引用文件/pages/admin/main.html(例如)使用相对路径,您将使用:

src="../../assets/js/jquery.js"

Suppose you move the file to a child directory. No changes would be needed for with the original rooted path, but the relative one would need to change to:

假设您将文件移动到子目录。原始根路径不需要更改,但相对路径需要更改为:

src="../../../assets/js/jquery.js"

回答by Blender

Adding on @Oded's answer, the slash makes the URL absolute.

添加@Oded 的回答,斜线使 URL 成为绝对值。

For example:

例如:

/foo/bar/baz.css

This translates to:

这转化为:

http://www.example.com/foo/bar/baz.css

But without the slash, things become a bit different:

但是没有斜线,事情变得有点不同:

foo/bar/baz.css

This tells the browser to look in the currentfolder (not the root folder) for the directory fooand then the subsequent directories and the file.

这告诉浏览器在当前文件夹(而不是根文件夹)中查找目录foo,然后查找后续目录和文件。



Also, take for instance this HTML:

另外,以这个 HTML 为例:

<script type="text/javascript" src="foo.js"></script>

If you move the HTML file into another folder, then the script will not load, as foo.jsisn't being moved with the HTML file.

如果您将 HTML 文件移动到另一个文件夹中,则脚本将不会加载,因为foo.js不会随 HTML 文件一起移动。

But if you use an absolute URL:

但是如果你使用绝对 URL:

<script type="text/javascript" src="/foo.js"></script>

Then the JS file is loaded EXACTLYfrom http://www.example.com/foo.jsno matter where the HTML file is.

然后JS文件加载EXACTLYhttp://www.example.com/foo.js无论身在何处的HTML文件。

回答by Si Griffiths

This is to ensure the asset comes from the "root" of the web server.

这是为了确保资产来自 Web 服务器的“根”。

e.g. Host is www.example.com URL becomes www.example.com/assets/js/jquery.js

例如主机是 www.example.com URL 变为 www.example.com/assets/js/jquery.js

I do this with project I want to ensure live on their own virtual host.

我用我想确保在他们自己的虚拟主机上直播的项目来做到这一点。

The issue really comes down to where those assets are being included. For example if the asset is being included from /help/pages/faq then the developer can be sure the path will work correctly when the site is hosted on a non changing host, e.g. example.com.

问题实际上归结为这些资产的包含位置。例如,如果资产是从 /help/pages/faq 包含的,那么开发人员可以确保当站点托管在不变的主机上时,路径将正常工作,例如 example.com。

The issue of using relative paths, 'assets/js/jquery.js' is that if the assets are included from the /help/pages/faqs then the path becomes relative to that starting point, e.g. /help/pages/faqs/assets/js/jquery.js

使用相对路径的问题,'assets/js/jquery.js' 是,如果资产是从 /help/pages/faqs 中包含的,那么路径就变得相对于那个起点,例如 /help/pages/faqs/assets /js/jquery.js

Hope that helps

希望有帮助