正确设置< a href ="">当它是一个本地文件 [英] The right way of setting <a href=""> when it's a local file

查看:271
本文介绍了正确设置< a href ="">当它是一个本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图链接到本地​​文件。我已经设置了href,如下所示:

 < a href =file:// C:/ path / to / file /file.html\">链接锚点< / a> 




  • 在Firefox中,当我右键点击打开链接当我点击右键并复制链接位置,然后手动打开一个新的选项卡并粘贴复制的链接,工作正常。所以看来我的 file:// 语法没问题。我也试过了3个斜线,比如 file:/// ,但结果是一样的。

  • 方案

根据定义,file:URLs是依赖于系统的,而且它们几乎没有用处。当你在本地使用的时候,你的例子中的一个URL是可用的,即链接页面本身在用户的计算机中。但是浏览器通常拒绝跟随它使用HTTP协议获取的页面上的文件:链接,以便页面自己的URL是http:URL。当你点击这样一个链接时,没有任何反应。目的大概是安全的:防止远程页面访问访问者的计算机中的文件。 (我认为这个功能是在Mozilla中首先实现的,然后复制到其他浏览器中)。

因此,如果你在你的计算机上使用HTML文档,URL:URLs ,但是它们的语法中存在与系统相关的问题(如何在这样的URL中编写路径名和文件名)。

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


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

<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.

What am I doing wrong?

解决方案

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.)

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).

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,

这篇关于正确设置&lt; a href =&quot;&quot;&gt;当它是一个本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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