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

221,370

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>
Share:
221,370

Related videos on Youtube

sameold
Author by

sameold

Updated on July 09, 2022

Comments

  • sameold
    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 like file:/// but it's the same result.

    What am I doing wrong?

  • sameold
    sameold over 11 years
    I 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 to C:/ +1 though for trying to help.
  • ArjaaAine
    ArjaaAine almost 10 years
    Thank 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
    Jenna Leaf over 8 years
    even you do not have the ideal directory structure, you can use base tag as shown inside my answer below
  • Pontis
    Pontis over 5 years
    Is 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
    elersong over 4 years
    It might be helpful to the user if you explain why that should do it. I know I'd like to learn why that works.