Question Details

No question body available.

Tags

javascript

Answers (2)

Accepted Answer Available
Accepted Answer
November 2, 2025 Score: 4 Rep: 152 Quality: High Completeness: 50%

Add a short delay before redirecting

You can use a small setTimeout() delay - e.g., 100–500 ms, to ensure the file download starts before redirecting

Export and Go

document.getElementById("exportButton").addEventListener("click", function() { exportData();

setTimeout(() => { window.location.href = "https://example.com/thankyou"; }, 500); });

function exportData() { const data = "name,age\nJohn,25\nJane,30"; const blob = new Blob([data], { type: "text/csv" }); const url = URL.createObjectURL(blob);

const a = document.createElement("a"); a.href = url; a.download = "data.csv"; document.body.appendChild(a); a.click(); document.body.removeChild(a);

URL.revokeObjectURL(url); }

November 2, 2025 Score: 3 Rep: 1,671 Quality: Medium Completeness: 60%

Assuming your server-to-client connection has a big latency, the setTimeout method can be a bit unreliable.

Alternative option 1: in-line the redirect target in an , and un-hide the iframe when the redirect is to occur. Besure to style the background of white (or whatever color suitable for your webpage's color scheme) because by default it's transparent.

document.getElementById("exportButton").addEventListener("click", function() { exportData(); // function that creates and downloads a CSV window.frames["thank-you"].location.href = "https://example.com/thankyou"; document.getElementById("thank-you").hidden = false; });

Alternative option 2: create a Blob URL and redirect to a thank-you page with a parameter having the value of that URL (be sure to encodeURIComponent), and make the "thank-you" page download from that Blob URL instead.

function exportData() { const data = "name,age\nJohn,25\nJane,30"; const blob = new Blob([data], { type: "text/csv" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = "https://example.com/thankyou?csvbody="+encodeURIComponent(url); a.target = "self"; // or "blank", whichever you prefer. document.body.appendChild(a); a.click(); document.body.removeChild(a); }

Then in the thank-you page:

window.onload = function() { var params = new URLSearchParams(window.location.search); const a = document.createElement("a"); a.href = params.get('csvbody'); a.download = "data.csv"; document.body.appendChild(a); a.click(); document.body.removeChild(a); }