I came across this handy bit of code that will redirect a user on wordpress to a new page after a download has started. I just wanted to share this bit of code as I will no doubt be using this in the future for one of my many sites.
So the link to the file is actually a link to another page with a query string variable (in this case, the name of the download file without .zip on it.
i.e. http://www.premiumpixels.com/download/?file=menu
What you are seeing is the contents of /download – try without the ?file=menu on the end….!
So the user clicks through to the next page (which could be anything of course – you could easily have some logic in there that detected the actual file being downloaded and change the contents of the page based on that) and then the script above fires off, reads the query string and downloads the file!
jQuery(function () {
// get the GET variables
var theme = getUrlVars();
var downloadLink = 'http://cdn.premiumpixels.com/uploads/' + theme['file'] + '.zip';
if(theme['file'])
{
jQuery('#downloadLink').attr('href', downloadLink);
delayedDownload();
}
function delayedDownload()
{
timeoutID = window.setTimeout(downloadTheme, 1000);
}
function downloadTheme()
{
window.location.replace(downloadLink);
//window.open(downloadLink,'','menubar=1,location=1,toolbar=1,width=600,height=500');
}
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
});











Comments