+ Start a Discussion
SS KarthickSS Karthick 

Redirect one viusalforce page to another page automatically using javascript

Hi folks,
    Can anyone tell me how to redirect one visualforce page to another visualforce page automatically ?
below is my visualforce page :
<apex:page standardController="Account" extensions="AccountDownload" 
contentType="application/msWord/#Test-{!NOW()}.doc"
   recordSetVar="accounts" sidebar="false"  cache="true"> 
    
    <html xmlns:w="urn:schemas-microsoft-com:office:word">
        <head>
            
        
        </head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
         
        <body>                       
            <apex:form >
                <apex:pageBlock title="Archiver For Account">
                     <div style="text-align:left" >          
                         <apex:pageBlockTable value="{!accounts}" var="ac" >
                             <apex:column headerValue="AccountName">
                                 <apex:outputText >{!ac.Name}</apex:outputText>
                             </apex:column>
                             <apex:column headerValue="Account Number">
                                 <apex:outputText >{!ac.AccountNumber}</apex:outputText>
                             </apex:column>
                             <apex:column headerValue="AccountType">
                                 <apex:outputText >{!ac.Type}</apex:outputText>
                             </apex:column>
                             <apex:column headerValue="Phone Number">
                                 <apex:outputText >{!ac.Phone}</apex:outputText>
                             </apex:column>                                       
                         </apex:pageBlockTable>  
                      </div>           
                    </apex:pageBlock>
    
               <apex:outputText value="hi dude,"/>
           </apex:form>
           
        </body>
        <script>
 
            function redirect(){
                window.location.href='https://c.ap1.visual.force.com/apex/AccountasTextfile'; 
            }
 
            window.onload = window.setTimeout(redirect(),5000);
 
        </script>
    </html>
</apex:page>

My question is once the document is downloaded then it will automatically redirect to another page as mentioned above.
I have tried using action attribute,calling redirect method from constructor. but nothing happen.
Finally I got the idea is using javascript we can redirect automatically.
but it dosent work for me.

Someone please tell me what I have to modify.



Thanks in advance
Karthick
Nishant SharmaNishant Sharma
Just replace 
window.onload = window.setTimeout(redirect(),5000);

with
 
window.onload = redirect;

For your reference:
https://thechamplord.wordpress.com/2014/07/04/using-javascript-window-onload-event-properly/
SS KarthickSS Karthick
@Nishant Sharma:  It does not work for me.
Is ther any other way to redirect automatically once this download is complete.

Thanks in advance
Karthick
Nishant SharmaNishant Sharma
I suspect two areas:

1. Hardcoded domain.
Replace below code:
window.location.href='https://c.ap1.visual.force.com/apex/AccountasTextfile';

with
window.location.href='/apex/AccountasTextfile';

2. Content Type is word document.
contentType="application/msWord/#Test-{!NOW()}.doc"

Try once by removing this as well and please share behaviour.
Aniruddha PatilAniruddha Patil

HI Karthick,
I am also working on similar kind of requirement.
Did you get any method to redirect to another VF page ??
 

Rick YangRick Yang
Cause "developer mode" is on. Previewing page will open in vf editor.
 
satish pal 1satish pal 1
<script>
setTimeout(function()
{
window.location.href='page url''
},1000);
  </script>