How to wait for location.href to complete in JavaScript?

Below is the JavaScript code below.

Step 1 : go to .pdf, .doc, .exe or something that is not html native. If location.href has taken the browser window, then there is no need to do step 2. (PDF files usually take the browser window). Most other functions start the boot manager process. For example, .exe. But there are some things, such as text documents, that can be downloaded or displayed directly in the browser window depending on the browser settings. I want him to do what hef.location would do.

Step 2 But if it downloads a file such as .exe after completing this process, go to the main page.

Or the solution to wait only 5 seconds between steps 1 and 2 seems to work most of the time. But with a slower connection, this does not always work. Then it goes to the home page without ending the first call to href.location, and they never see the PDF file or see the home page.

FYI ... The reason I wrap them in setTimeOut is due to this firefox problem. Stack Overflow: 864633 target-on-document-location-href-without-clobbering-history

My question is: Is there a way to find out when the location.href process is complete?

<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> 
+4
source share
3 answers

You have two options, as far as I can tell

  • A link to the file in a pop-up window (browsers should usually close this if they find a downloadable file) and at the same time redirect to the landing page
  • Create some kind of dynamic function that links to the landing page and initiates its loading, but this may not be applicable in your case

Pure javascript is not possible, and tunneling the download through the server-side script also cannot handle redirects. You will have to go with a solution inbetween I'm afraid

0
source

I do not believe that you can do what you ask in JavaScript. When you install something in window.location, you basically tell the browser to load the specified url in the current window. When the browser does this, all the code on the previous page stops working.

Here are a few possibilities from the head:

  • Download the external file in a popup window or in a new window. I have not tried this, so I'm not sure how it will work in all browsers.
  • Use any server language that you use to serve the file. After that, you can redirect the client after the file is sent.
+2
source

As others have argued, there is no onDownloadComplete event in Javascript. However, if you can display different files at boot time, you can use the following technique:

  • on page A, you have a link to page B
  • on page B, display the content that you want to display to users during file download.
  • inside the element for page B, add a META tag that will actually start loading, i.e.

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

The end result is that the browser continues to display content from (2), but also prompts the user to save the download using the "Save As ..." dialog box.

Many download sites, such as CNet and Sourceforge, use a similar technique.

+1
source

All Articles