为什么我的浏览器仍然可以打开一个不是通过静态文件服务器提供的 html 文件? [英] why can my browser still open an html file not served through a static file server?

查看:49
本文介绍了为什么我的浏览器仍然可以打开一个不是通过静态文件服务器提供的 html 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道这是如何/为什么工作的,当我制作一个简单的 html 文件并在一些 css 中链接,然后将我的 html 文件拖到浏览器中时,我不需要静态 Web 服务器来查看文件.

Just wondering how/why this works, when I'm making a simple html file and linking in some css, then dragging my html file into the browser, no static web server is needed for me to view the file.

为什么会这样..

我正在查看浏览器的网络选项卡,并没有对 css 文件发出请求,但我的浏览器仍然完美地显示它..

I'm looking at my browser's network tab, and no request is made for the css file, and my browser still displays it perfectly..

有没有办法在 web 上不用静态文件服务器来处理 html、css、js 文件,比如将文件拖放到浏览器中?

Is there a way to do without a static file server on the web for html, css, js files, like when dragging and dropping a file into a browser?

回到这里并要求基础知识..

Just going back and requestionning basics here..

提前致谢!

推荐答案

因为到您的 CSS 文件的链接是相对的,并且您的 CSS 文件可以在本地访问.浏览器可用于访问本地文件,而不仅仅是 Internet 上的文件.

Because the link to your CSS file is relative, and your CSS file is accessible locally. Browsers can be used to access local files, not just files on the Internet.

使用链接时,您可能只看到引用的文件名,例如:

When working with links, you may see just the name of the file referenced, as such:

<a href="file.html">Link</a>

这称为相对链接.file.html 相对于链接到它的文档所在的位置.在这种情况下,这两个文件将位于同一文件夹中.

This is known as a relative link. file.html is relative to wherever the document is that is linking to it. In this case, the two files would be in the same folder.

还有第二种类型的链接,称为绝对 URL,其中指定了完整路径.

There's a second type of link, known as an absolute URL, where the full path is specified.

考虑一个典型的绝对网站链接:

Consider a typical absolute website link:

<a href="http://website.com/file.html">Link</a>

对于本地文件,这基本上是:

With a local file, this would essentially be:

<a href="file://[YOUR WEBSITE]/file.html">Link</a>

文件协议可用于访问本地文件.

考虑到主页(大概是 index.html)和 file.html 将位于 Web 服务器和本地计算机上的同一文件夹中,<a href="file.html">Link</a> 适用于任一场景.事实上,使用相对链接,第二个文件的位置是根据第一个文件的位置自动确定的.在我的示例中,index.html 将位于 file://[YOUR WEBSITE]/index.html,因此您的浏览器足够智能,可以查看 file://[YOUR WEBSITE]/ 搜索任何相对 URL 时.

Considering both the homepage (presumably index.html) and file.html would live in the same folder on both a web server and your local machine, <a href="file.html">Link</a> would work for either scenario. In fact, with a relative link, the location of the second file is automatically determined based on the location of the first file. In my example, index.html would live at file://[YOUR WEBSITE]/index.html, so your browser is smart enough to known to look in file://[YOUR WEBSITE]/ when searching for any relative URLs.

请注意,同样的情况适用于任何其他文件!<link> 标签将以完全相同的方式查找文件——包括您的样式表 :)

Note that the same scenario applies to any other file! <link> and <script> tags will look for files in the exact same way -- that includes your stylesheet :)

希望这会有所帮助!

这篇关于为什么我的浏览器仍然可以打开一个不是通过静态文件服务器提供的 html 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆