Problems, need help? Have a tip or advice? Post it here.
11 posts Page 1 of 2
Hello all, i have this Javascript code for my redirect download page:
Code: Select all
<script type="text/javascript">
function DownloadAndRedirect()
{
   var DownloadURL = "<cms:show download_link/>";
   var RedirectURL = "/redirect.php";
   var RedirectPauseSeconds = 1;
   location.href = DownloadURL;
   setTimeout("DoTheRedirect('"+RedirectURL+"')",parseInt(RedirectPauseSeconds*1000));
}
function DoTheRedirect(url) { window.location=url; }
</script>

Redirect was fine, but i can't get the same file when i hit the link "Click Here" in the page redirected from origin download button "If your download does not start after 30 seconds, Click here"

Any idea? i want the same file at the redirect page from the previous download page.

Here is my download button link:
Code: Select all
<a href="javascript:DownloadAndRedirect()" target="_blank" style="border-radius:4px;padding: 20px;" class="btn btn-success btn-lg btn-block"><i class="fa fa-download" aria-hidden="true"></i> Download</a>

Thanks in advance.
I suggest to amend the logic. See, visitor clicks on link and anyway gets redirected to the second page.

For a quick and easy solution, you can first redirect user to that page, then on that page instruct the browser to start download the file immediately and display a link for a manual download.

With this solution, 2 operations - link generation and downloading - would be consolidated in one file.


Also, another suggestion - change the HTML of the clicked link on page1 to the "30 seconds" text. This way there is no need to have extra page at all.
trendoman wrote: I suggest to amend the logic. See, visitor clicks on link and anyway gets redirected to the second page.

For a quick and easy solution, you can first redirect user to that page, then on that page instruct the browser to start download the file immediately and display a link for a manual download.

With this solution, 2 operations - link generation and downloading - would be consolidated in one file.


Also, another suggestion - change the HTML of the clicked link on page1 to the "30 seconds" text. This way there is no need to have extra page at all.

Thanks trendoman for the solution. I got it now.

But, little bit confuse, how to make Manual link ("Click here") link on the second page exacly the same link file download with the first page? right now, it always downloading my first post. Here's my cms pages:
Code: Select all
<cms:pages masterpage='article.php' limit='1'>
<span class="error-button">If your download does not start after 30 seconds,  <a href="<cms:show download_link/>">Click here</a></span>
</cms:pages>
There are at least 2 ways to do it - via visible querystring or via invisible flash. Querystring (page_id, for example) is added to redirection url in JS script, I will skip posting code for this solution. Let's see another solution:

2. With flash thing gets easier:

On the first page:
Code: Select all
<cms:pages masterpage='article.php' limit='1'>
    <span class="error-button"><a href="<cms:show download_link/>">Click here</a></span>
    <cms:set_flash 'file_url' value=download_link />
</cms:pages>


On second page:

Code: Select all
<span class="error-button">If your download does not start after 30 seconds,  <a href="<cms:get_flash 'file_url' />">Click here</a></span>


More about set_flash, get_flash here viewtopic.php?f=5&t=7377
trendoman wrote: There are at least 2 ways to do it - via visible querystring or via invisible flash. Querystring (page_id, for example) is added to redirection url in JS script, I will skip posting code for this solution. Let's see another solution:

2. With flash thing gets easier:

On the first page:
Code: Select all
<cms:pages masterpage='article.php' limit='1'>
    <span class="error-button"><a href="<cms:show download_link/>">Click here</a></span>
    <cms:set_flash 'file_url' value=download_link />
</cms:pages>


On second page:

Code: Select all
<span class="error-button">If your download does not start after 30 seconds,  <a href="<cms:get_flash 'file_url' />">Click here</a></span>


More about set_flash, get_flash here viewtopic.php?f=5&t=7377

Thanks for the solution trendoman, sorry for the late reply.

But, i still get the different link download at the second page.

The second page is different template from the origin page anyway.
@setiawanfarlin, the code I provided would work only if what you posted was correct initially. I assume now, that you posted only code with a single link. Do you see the issue?

Code above would give a different link only if it runs several times with different links on the first page. I.e. you have many links to click. So the flash variable gets overwritten as many times as many links exist on page. To prove this, you can click on the latest link and it would produce correct result on the second template.
trendoman wrote: @setiawanfarlin, the code I provided would work only if what you posted was correct initially. I assume now, that you posted only code with a single link. Do you see the issue?

Code above would give a different link only if it runs several times with different links on the first page. I.e. you have many links to click. So the flash variable gets overwritten as many times as many links exist on page. To prove this, you can click on the latest link and it would produce correct result on the second template.


Exactly, thats whats happened.

Many thanks for the respond anyway, it was clearly make me understand it. So, what do you think the best solution for it now?
The best solution would be just like previously discussed - to send full file path to the other page.

Lucky you I found some old snippet that I used eariler in project and it partly helps. Use it with clicking event in JS (rewrite to use plain JS instead of jQuery).
Code: Select all
//Clicked link goes here
var file = ...
// Url for redirect
var redirectUrl = ...

// Make a form with hidden input
var form = $('<form action="' + redirectUrl + '" method="post" >' +
             '<input type="hidden" name="file" value="' +  file + '"/>' +
             '</form>');

// Attach this form to body and submit it
$('body').append(form);
form.submit();


Then on the destination page take the path and use it
Code: Select all
<cms:set file = "<cms:gpc 'file' />" />
<cms:if file >
    <a href="<cms:show file />" >... </a>
<cms:else />
    What are you doing on this page?
</cms:if>   
trendoman wrote: The best solution would be just like previously discussed - to send full file path to the other page.

Lucky you I found some old snippet that I used eariler in project and it partly helps. Use it with clicking event in JS (rewrite to use plain JS instead of jQuery).
Code: Select all
//Clicked link goes here
var file = ...
// Url for redirect
var redirectUrl = ...

// Make a form with hidden input
var form = $('<form action="' + redirectUrl + '" method="post" >' +
             '<input type="hidden" name="file" value="' +  file + '"/>' +
             '</form>');

// Attach this form to body and submit it
$('body').append(form);
form.submit();


Then on the destination page take the path and use it
Code: Select all
<cms:set file = "<cms:gpc 'file' />" />
<cms:if file >
    <a href="<cms:show file />" >... </a>
<cms:else />
    What are you doing on this page?
</cms:if>   


Thanks Trendoman, it work like a charms... :)
Welcome :)
Could you post full JS script to make this solution complete?
11 posts Page 1 of 2
cron