Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: How to download a file, then force a page reload?

As the title mentions, I am trying to download a file which is served with associated mime type via PHP script given by href URL, then reload the same page, but can't quite figure it out, here's what I have so far:

<a id="viewAttachmentLink" href="/path/to/script.php?id=123">View Attachment</a>

<script type='text/javascript'>
    jquery('#viewAttachmentLink').bind('click', function() {
        if (myFunction()) {
            window.location.href = "jquery(this).attr('href')";
            setTimeout(location.reload(), 400);
        } else {
            return false;
        }
    });
</script>

With this code, it will reload the page, but appears to not make the call to the PHP script.

like image 886
Mike Purcell Avatar asked Dec 05 '12 23:12

Mike Purcell


1 Answers

As mentioned in the comments, I was able to get around the issue by adding a target="_new" attribute to the link. So when the link was clicked it would send the request to the remote php script to another window, which would control the headers and start downloading the file, and the original window would reload as needed.

like image 115
Mike Purcell Avatar answered Oct 07 '22 00:10

Mike Purcell