多次使用同一链接的最佳做法是什么?

I'm trying to rationalize a website, and I have many links on it to the same document, so I want to create a JavaScript that return the URL of this document. This way, i could update the document and only have to change the URL in the function, not in all the occurrences of the link (it's a professional and internal website, with many links to official documents, that get updated often, out of my control, and each time i get to update links, i realize a while after that i forgot some, even by searching in all html files. the site is messy, was poorly written by many people, and that's why i'm trying to simplify)

My first idea was to use <A href="javascript:F_link_pdf()">link</A>, but everyone says it's a bad practice. i still don't see why, and I don't like to use the onclick as it doesn't work with middle click, and i want to let users decide how they open the doc.

Also, I want to use <A href="javascript:F_link_pdf()#page=39">link</A> to redirect to a specific page.

on top of this, what i tried so far is not working like I intend, so i would need some help, whether to come up with a better solution, or to make this work!

here is my js, with different versions:

function F_link_PDF() {
    // i was pretty sure this would work
    return "http://www.example.com/presentation.pdf" ;
}
function F_link_PDF_2() {
    document.write("http://www.example.com/presentation.pdf");
}
function F_link_PDF_3() {
    // i don't like this solution, as it doesn't open as user intended to
    location.href = "http://www.example.com/presentation.pdf" ;
}

this example is for a pdf document, but i could also need this for html, doc, ppt... and finally, i started with js because i'm used to, but I could also use other languages, php, asp, if someone says it's a better option

thanks in advance!

The hack way: Go about using JavaScript, however you run into potential issues with browsers not running it.

The better way: Use mod_rewrite / .htaccess to redirect previous (expired) requests to the new location of the resource. You could also use FallbackResource and provide a .php file that could provide the new resource based on criteria (you now have the power of PHP to decide where the Location header should go).

The best way1: Place those document references in a database table somewhere and reference them in the page using the table's current value. This creates a single place of "truth" and allows you to update the site from a global perspective. You could also, at a later date, provide search, tag, display a list, etc.

 

1Not implying it's the abosolute best, but it is certainly a better way than updating hard-coded references.

A server side programming language like php is a better option.

Here's example code that helps:

<?php
$link="http://www.example.com/files/document.pdf";
if ($_GET['PAGE'] == "downloads")
    {
      ?>
      This is a download page where you can download our flyer.
      <?php
      echo "<a href=\"".$link."\">Download PDF</a>";
    }

if ($_GET['PAGE'] == "specials")
    {
      ?>
      This is our store specials page. check them out. a link to the flyer is below.
      <?php
      echo "<a href=\"".$link."\">Download PDF</a>";
    }
?>

The code isn't 100% perfect since some text needs adjusting but what it does is it takes a parameter PAGE and sees that it is "downloads" or "specials" and if it is, it loads the appropriate page and adds the link to the download file. If you try both pages, the link to the download is exactly the same.

If the above php script is saved as index.php, then you can call each page with:

index.php?PAGE=specials   for the specials page
index.php?PAGE=downloads  for the download page

Once that works, then you can add another "if" section for another page to create but the most important line in each section is the last line of...

echo "<a href=\"".$link."\">Download PDF</a>";

...because it's taking a variable thats usable in every case in the script.

An advantage with using server side method is that people can view the site even with javascript disabled.