Html 当它是本地文件时设置 <a href=""> 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12021781/
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 02:19:58  来源:igfitidea点击:

The right way of setting <a href=""> when it's a local file

htmlfilefirefoxhyperlinkhref

提问by sameold

I'm trying to link to a local file. I've set href as follows:

我正在尝试链接到本地​​文件。我已将 href 设置如下:

<a href="file://C:/path/to/file/file.html">Link Anchor</a>
  • In Firefox, when I right click and "open link in new tab", nothing happens.

  • When I right click and "copy link location", then manually open a new tab and paste the copied link, it works fine. So it seems my file://syntax is fine. I've also tried it with 3 slashes like file:///but it's the same result.

  • 在 Firefox 中,当我右键单击并“在新选项卡中打开链接”时,没有任何反应。

  • 当我右键单击并“复制链接位置”,然后手动打开一个新选项卡并粘贴复制的链接时,它工作正常。所以看起来我的file://语法很好。我也试过用 3 个斜线,file:///但结果是一样的。

What am I doing wrong?

我究竟做错了什么?

回答by Jukka K. Korpela

By definition, file: URLs are system-dependent, and they have little use. A URL as in your example works when used locally, i.e. the linking page itself is in the user's computer. But browsers generally refuse to follow file: links on a page that it has fetched with the HTTP protocol, so that the page's own URL is an http: URL. When you click on such a link, nothing happens. The purpose is presumably security: to prevent a remote page from accessing files in the visitor's computer. (I think this feature was first implemented in Mozilla, then copied to other browsers.)

根据定义,file: URL 依赖于系统,它们几乎没有用处。您示例中的 URL 在本地使用时有效,即链接页面本身位于用户的计算机中。但是浏览器通常拒绝跟踪它使用 HTTP 协议获取的页面上的 file: 链接,因此该页面自己的 URL 是一个 http: URL。当您单击这样的链接时,什么也没有发生。目的大概是安全:防止远程页面访问访问者计算机中的文件。(我认为这个功能首先在 Mozilla 中实现,然后复制到其他浏览器。)

So if you work with HTML documents in your computer, the file: URLs should work, though there are system-dependent issues in their syntax (how you write path names and file names in such a URL).

因此,如果您在计算机中处理 HTML 文档,则 file: URL 应该可以工作,尽管它们的语法存在系统相关问题(如何在此类 URL 中写入路径名和文件名)。

If you really need to work with an HTML document on your computers and another HTML document on a web server, the way to make links work is to use the local file as primary and, if needed, use client-side scripting to fetch the document from the server,

如果您确实需要处理计算机上的 HTML 文档和 Web 服务器上的另一个 HTML 文档,则使链接起作用的方法是使用本地文件作为主要文件,如果需要,使用客户端脚本来获取文档从服务器,

回答by Petar Sabev

Organize your files in hierarchical directories and then just use relative paths.

在分层目录中组织您的文件,然后只使用相对路径。

Demo:

演示:

HTML (index.html)

HTML (index.html)

<a href='inner/file.html'>link</a>

Directory structure:

目录结构:

base/
base/index.html
base/inner/file.html
....

回答by Jenna Leaf

The href value inside the base tag will become your reference point for all your relative paths and thus override your current directory path value otherwise - the '~' is the root of your site

base 标签中的 href 值将成为所有相对路径的参考点,因此会覆盖当前目录路径值,否则 - '~' 是您网站的根目录

    <head>
        <base href="~/" />
    </head>

回答by Ashraf Abusada

This can happen when you are running IISand you run the html page through it, then the Local file systemwill not be accessible.

当您正在运行IIS并通过它运行 html 页面时,可能会发生这种情况,然后Local file system将无法访问。

To make your link work locally the run the calling html page directly from file browser not visual studio F5or IISsimply click it to open from the file system, and make sure you are using the link like this:

要使您的链接在本地工作,请直接从文件浏览器而不是 Visual Studio 运行调用 html 页面,F5或者IIS只需单击它即可从文件系统打开,并确保您使用的是这样的链接:

<a href="file:///F:/VS_2015_WorkSpace/Projects/xyz/Intro.html">Intro</a>

回答by David Schronce

Try swapping your colon :for a bar |. that should do it

试着用你的冒号:换一个酒吧|。应该这样做

<a href="file://C|/path/to/file/file.html">Link Anchor</a>

回答by Priyanka Davle

../htmlfilename with .html User can do this This will solve your problem of redirection to anypage for local files.

../htmlfilename with .html User can do this 这将解决您重定向到本地文件的任何页面的问题。