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

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

问题描述

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

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>

  • 在 Firefox 中,当我右键单击并在新标签页中打开链接"时,没有任何反应.

    • In Firefox, when I right click and "open link in new tab", nothing happens.

      当我右键单击并复制链接位置",然后手动打开一个新选项卡并粘贴复制的链接时,它工作正常.所以看起来我的 file:// 语法很好.我也尝试过使用 3 个斜杠,例如 file:///,但结果相同.

      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.

      我做错了什么?

      推荐答案

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

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

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

      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 文档和 Web 服务器上的另一个 HTML 文档,则使链接起作用的方法是使用本地文件作为主要文件,如果需要,使用客户端脚本来从服务器获取文档,

      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=""&gt;当它是本地文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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