0

page should be redirected after the download starts, but I have the code like whether the download start or not, it is redirecting after the setTimeout. Is there any solution so that we can write a condition like after download start or complete , we can redirect to desired page. Thanks in advance..

<?php
while($row1=mysql_fetch_array($result))
{
$name=$row1['name'];
$type=$row1['type'];
?>
<div class="rect" id="d1">
<img alt="down-icon" src="down-drop-icon.png" align="left" width="20" height="20" />
<a href="download.php?filename=<?php echo $name ;?>" >
<?php echo $name ;?></a>

<script type="text/javascript">
var howLongToWait = 15; //number of seconds to wait
var urlOfRedirectLocation = 'http://gomasti.in/p/gomasti/';
function startRedirect() {
window.top.location.href = urlOfRedirectLocation;
}
setTimeout('startRedirect()', howLongToWait * 1000);
</script>
</div>
<?php }?>
chandu577
  • 3
  • 1
  • 6
  • A not-so-elegant solution (for the visitor) is to open a popup window which starts the download, which will close after that (by itself). Then just catch the close event of the popup (pretty sure that's possible), and redirect using JS. That's the way the Chrome download page does it (at least in some browsers). – Eduard Luca Oct 10 '14 at 08:56
  • Could be helpful: http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – ilpaijin Oct 10 '14 at 09:14

1 Answers1

0

You want to activate the timer when the user clicks the link:

<a href="download.php?filename=<?php echo $name ;?>" onclick="setTimeout('startRedirect()', howLongToWait * 1000);"><?php echo $name ;?></a>

The <script> code needs to be before while loop, define the function and variable only once.

Marek
  • 7,337
  • 1
  • 22
  • 33