9 Replies Latest reply on Apr 23, 2014 1:13 PM by AaronRTor

    Stop Browser from Caching one link

    kerry31 Level 1

      Hey all,


      Quick question for you. I have a link on my website that is going to a pdf file that gets updated once a week. The problem I see all my users having is the browser is caching that link and if they force refesh it works but I want to make this easier on them. The way I have it setup is the link goes to http://www.metrocog.org/sermon.pdf I just replace the sermon.pdf file every week on the server that way I dont have to change the link every week. I know I can force a browser not to cache the page but I'm wondering if I can force the browser not to cache just that link?



        • 1. Re: Stop Browser from Caching one link
          Ken Binney Level 6

          How about not linking directly to the PDF file on your main pages but placing that PDF link on a new "gateway" page with nocache and or refresh metatags?

          • 2. Re: Stop Browser from Caching one link
            kerry31 Level 1

            Hey Ken good to hear from you again!

            That sounds like an option I could live with, not perfect but I will give it a try.

            • 3. Re: Stop Browser from Caching one link
              ajatix.com Level 3

              You can also add an arbitrary parameter to the link each time, so the url looked something like sermon.pdf?p=23863

              You don't have to process this parameter on the server side, the whole purpose is to prevent caching


              It can be just a random number

              <a onmouseover="this.href='sermon.pdf?p='+ Math.random()" href="sermon.pdf">PDF</a>


              or a timestamp

              <a onmouseover="this.href='sermon.pdf?p='+ new Date().getTime()" href="sermon.pdf">PDF</a>


              For simplicity I put JavaScript directly inside the anchor tag, you can move it out to make less obtrusive.



              1 person found this helpful
              • 5. Re: Stop Browser from Caching one link

                Hi ajatix (and all),


                Thanks you so much for this solution.  I have sent days trying to figure this one out cache busting for PDFs.  I wonder if you might have a little more advice.


                This is how I originally implemented your code suggestion.

                <a onmouseover="this.href='images/file.pdf?p='+ new Date().getTime()" href="images/file.pdf" target="new">Production Schedule</a>


                This is a 45 page document and I would like to include jump to links for the file using Open PDF paramitors #page=33&pagemode=thumbs.


                I can get the Open parameters to work and the unique query string, but I cannot get them to work together.  Here is the code I have tried:


                <a onmouseover="this.href='images/file.pdf?p='+ new Date().getTime()#page=33&pagemode=thumbs" href="images/file.pdf#page=33&pagemode=thumbs" target="new">January</a>  




                <a onmouseover="this.href='images/pc2014.pdf#page=15&pagemode=thumbs?p='+ new Date().getTime()" href="images/pc2014.pdf#page=15&pagemode=thumbs" target="new">September</a>


                With either of these options the browser does not recognize the cache buster.


                Any thoughts?




                • 6. Re: Stop Browser from Caching one link
                  ajatix.com Level 3

                  I think the URL string should use the following pattern:




                  The question mark precedes all the parameters, parameters are separated by '&', the '#' part (fragment ID) should be the last part in the URL string, e.g.


                  <a onmouseover="this.href='images/pc2014.pdf?pagemode=thumbs&p=' + new Date().getTime() + '#page=15'" href="images/pc2014.pdf?pagemode=thumbs#page=15" target="new">September</a>

                  1 person found this helpful
                  • 7. Re: Stop Browser from Caching one link
                    ajatix.com Level 3

                    I just took a look into 'Parameters for Opening PDF Files' document, and it seems the 'pagemode' parameter should be a part of the 'fragment_id'.


                    So your first code should be about right:


                    <a onmouseover="this.href='images/file.pdf?p='+ new Date().getTime()#page=33&pagemode=thumbs" href="images/file.pdf#page=33&pagemode=thumbs" target="new">January</a>


                    It just may need to be slightly corrected:


                    <a onmouseover="this.href='images/file.pdf?p='+ new Date().getTime() +'#page=33&pagemode=thumbs'" href="images/file.pdf#page=33&pagemode=thumbs" target="new">January</a>

                    1 person found this helpful
                    • 8. Re: Stop Browser from Caching one link
                      AaronRTor Level 1

                      Hi ajatix,


                      Thanks for your response.

                      When I use the Open Parameters the date stamp is still not being added to the link (i.e. there is no cache busting).

                      I am not sure how to fix this.


                      • 9. Re: Stop Browser from Caching one link
                        AaronRTor Level 1

                        Hi ajatix,


                        thanks again.  i went back and tried your suggestion from post 6 and it worked.  Putting the date in the middle between the pagemode and the page number seems to be the trick.