views:

195

answers:

3

The JavaScript below code has two steps.

Step 1: go to .pdf, .doc, .exe or something which not html native. If location.href has took over the browser window then there is no need to do step 2. (PDFs usually take over browser window). Most other things kick off a download manager process. Such as .exe. But there are some things such as word docs that could be either a download or show right in the browser window depending on browser setup. I want it to do what hef.location would make it do.

Step 2: But if it is downloading a file such as an .exe after that process is done then go to home page.

Or solution for just waiting 5 seconds between steps 1 and 2 seem to work most of the time. But on a slower connection it doesn't always works. Then it goes to home page without finishing the first href.location call and they never see the PDF and just see home page.

FYI...The reason I am wrapping them in setTimeOut is related to this firefox issue. Stack Overflow: 864633 assigning-to-document-location-href-without-clobbering-history

My question: Is there a way to tell when the location.href process gets completed?

<script language="JavaScript"><!--
function windowOnLoad() {
    setTimeout(function(){
       location.href='/someurl/something.pdf';  //sometimes this is .doc file
    },0);
    setTimeout(function(){
       location.href='/homepage';
    },5000);
    return false;
}
//-->
</script>
+1  A: 

I don't believe it is possible to do what you are asking in JavaScript. When you set something to window.location you are basically telling the browser to load the the specified url in the current window. When the browser does that all the code on the previous page ceases to operate.

Here are a couple possibilities from the top of my head:

  • Load the external file in a popup or a new window. I haven't tried that so I'm not sure how it will work in all the browsers.
  • Use whatever server side language you are using to serve up the file. You will then have the ability to redirect the client after the file has been served.
sheats
+1  A: 

As others have stated, there is no 'onDownloadComplete' event available to Javascript. However, if you are amenable to displaying a different while the download is occurring, you can use the following technique:

  1. on page A, have a link to page B
  2. on page B, display the content that you want to show users while the file is downloading
  3. inside the element for page B, add a META tag that actually starts the download, i.e.,

    <head>
        <meta http-equiv="refresh" content="1;url=http://server.com/document.doc" />
    </head>
    

The end result is that the browser will continue to show the content from (2), but also prompt the user to save the download via the 'Save as...' dialog box.

Many download sites like CNet and Sourceforge use a similar technique.

johnvey
A: 

You have two options as far as i can tell

  • Link to the file in a popup window (browsers should normally close this themselves if the only thing they find is a downloadable file) and at the same time redirect to the destination page
  • Build in some dynamic function that links to the destination page and initiates the download there but that might not be usefull in your case

Pure javascript this is impossible and tunneling a download through a serverside script can't handle redirects either. You'll have to go with a solution inbetween i'm afraid

ChrisR