Reputation: 2068
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>
Upvotes: 6
Views: 17422
Reputation: 14447
You have two options as far as i can tell
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
Upvotes: 0
Reputation: 5151
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:
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.
Upvotes: 1
Reputation: 34662
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:
Upvotes: 2