dsbc80836 2015-03-12 15:48
浏览 14
已采纳

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

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!

  • 写回答

2条回答 默认 最新

  • drj58429 2015-03-12 16:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式