The right way of setting <a href=""> when it's a local file
Solution 1
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,
Solution 2
Organize your files in hierarchical directories and then just use relative paths.
Demo:
HTML (index.html)
<a href='inner/file.html'>link</a>
Directory structure:
base/
base/index.html
base/inner/file.html
....
Solution 3
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
<head>
<base href="~/" />
</head>
Solution 4
This can happen when you are running IIS
and you run the html page through it, then the Local file system
will not be accessible.
To make your link work locally the run the calling html page directly from file browser not visual studio F5
or IIS
simply click it to open from the file system, and make sure you are using the link like this:
<a href="file:///F:/VS_2015_WorkSpace/Projects/xyz/Intro.html">Intro</a>
Related videos on Youtube
sameold
Updated on July 09, 2022Comments
-
sameold almost 2 years
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 likefile:///
but it's the same result.
What am I doing wrong?
-
numaroth almost 10 yearsI think the problem is with Firefox. As a security feature they prevent remote files from being able to access local files. There are workarounds, but they seem to be implemented through the browser. More information can be found here: kb.mozillazine.org/Links_to_local_pages_do_not_work
-
Mawg says reinstate Monica over 8 years
-
sameold over 11 yearsI can't seem to do it this way, because the data folder and the scripts are in different locations. I tried doing
../../etc
but it wouldn't go beyond the homepage of the site where the script is. It wouldn't go all the way up toC:/
+1 though for trying to help. -
ArjaaAine almost 10 yearsThank you for this.. I had been racking my head as to why my file links were not working. This needs to be marked as Answer!
-
Jenna Leaf over 8 yearseven you do not have the ideal directory structure, you can use base tag as shown inside my answer below
-
Pontis over 5 yearsIs there any possibility within HTHML5 or CSS to inherit either content of the
<a>
tag or the destination from the respective counterpart? I'm linking to files and would like to ensure that the displayed link corresponds to the filename. -
elersong over 4 yearsIt might be helpful to the user if you explain why that should do it. I know I'd like to learn why that works.