how to redirect to same page after download happens

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
chandu577
Forum Newbie
Posts: 1
Joined: Wed Oct 08, 2014 2:00 am

how to redirect to same page after download happens

Post by chandu577 »

I redirected to download page (I used window.location() or window.location.href() or replace()), but after download happens it should again come back to same page. I tried using setTimeout, but in vain. Another thing I dont have a chance to write redirect in download.php. Is there any solution for this requirement. Thanks in advance... Here is my sample code...

Code: Select all

<html>
<head>
<meta http-equiv="refresh" content="5; URL=index.php">
<script type="text/javascript" language="JavaScript">
function doSomething(color)
{
//do something nice with params  
document.body.style.background = color;
//alert('Hi......');
/*global window */
/*window.location = 'http://all-free-download.com/free-photos/in_love_cosmos_flower_garden_220378_download.html';*/
window.location.href = 'http://all-free-download.com/free-photos/in_love_cosmos_flower_garden_220378_download.html';
/*return false;*/
//header("location:javascript://history.go(-1)");
window.history.back(-1);
window.onload = function() { setTimeout("redirectPage()",3000); 
/*return false;*/
window.location.replace("http://google.com");
document.body.style.background = red;
window.setTimeout(redirectPage(), 500);
}

function redirectPage(){
window.location='http://google.com';
}

function download(){
var url = 'http://all-free-download.com/free-photos/in_love_cosmos_flower_garden_220378_download.html';
var htm = '<iframe src="' + url +'" onload="downloadComplete()"></iframe>';
document.getElementById('frameDiv').innerHTML = htm;
}
</script>
</head>
<body>
This page does call a JavaScript function when the page is loaded, 
without using the onload() event call.

<script type="text/javascript" language="JavaScript">
doSomething('blue');
</script>
</body>
</html>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how to redirect to same page after download happens

Post by pickle »

Can't be done. Your download page isn't acting like a web-page, it's acting like a file. When downloading a file - the headers get sent, then everything else afterward is considered file data. You can't put javascript before the file, because outputing javascript will send headers - breaking your download. You can't put a redirect after the file data because headers (ie: redirect headers) can't be sent after output has started.

If you have a webpage that has a link to a download, then when the user clicks the link a dialog should appear asking them what to do with it. Regardless of what they choose, the dialog will eventually go away, the browser will start downloading the file, and the user remains on the page they were on when they clicked the link. So I'm not really sure how a user's browser can navigate from a webpage to the URL of a file, if that file is being force downloaded.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply