views:

226

answers:

4

I am currently using the meta http-equiv='Refresh' to automatic start PDF downloads. however this has different effect on each of the browsers.

The main problem is with IE (6 & 7) once the user is redirected to the PDF if the user click the back button they are sent back to the page that initiated the download and then promptly redirect to the PDF again.

Firefox on windows has a better affect but still not perfect the user gets to the download page and is redirected to the PDF. However if they use the back button the completely miss the download page.

Currently Firefox on Linux has the exact effect i want. Being that the download page redirects you to the PDF and the back button from the PDF sends you back to the download page and there you stay.

I have tried using the javascript setTimeout() function to delay a pop-up of the PDF however the delay does not seem to work in any browser and the pop-up does not seem to work in IE7.

Has anyone implemented an elegant solution to the problem that i could use?

Thanks in advance for you help

+1  A: 

Referrer

You could check the referrer:

if (document.referrer != "http://homepage.com/file.pdf") {
    // go to the file
}

It's a little bit more complex because you have to compare to the absolute URL of the referrer, but this shouldn't be a problem.

Cookies

You could set a cookie the first time you enter the download page. If the cookie is set (for every file another entry in the cookie) then you shouldn't redirect.

A disadvantage is that if someone reloads the page (because he wants to see the file a second time) he can't access the file anymore. There are some possibilities around it:

  • Set a timestamp which expires in the cookie.
  • Add a link to the page which the user can click manually.

Setting/Getting a cookie:

if (document.cookie && document.cookie == "I was here!") {
    alert(document.cookie);
} else { // set a cookie
    document.cookie = "I was here!";
}

Javascript can redirect like this:

window.location.href = "http://www.google.com";
Georg
that means that if you actually want to open a document more than once, it will not get displayed (you've seen it once!).
tpk
A: 

use a javascript popup

this way when the user accesses the pdf, it's in a different browser, which you can close. once you do that, you can continue navigating through the site.

tpk
A: 

If I understood you need right, you want to do something like file-shares behavior: user waits few seconds with counter and after timeout you are redirecting him to the page wich starts download.

Maybe you should simply show link to download page instead of redirecting? This page should send headers "force-download" to avoid browser navigating on it. If someone will try to access this page directly -- check referer or headers or any other protection you can imagine (like dynamic links), maybe you already have one.

This solution will allow you to avoid JS magic and should make your code cross-browser without troubles.

Sergii
A: 

You can add an iframe to the DOM via javascript with src set to the url you want to download. The browser will not change page at all, the download will just start. It can be hidden using CSS and the user will never even know it's there.

joegester