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

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换