I have a Visualforce page that is embedded on the detail page of Opportunities.
Within the page is a command button that invokes a method in the backing controller extension.
Once the backing method is complete, how can I redirect the user to another page?
I can return a PageReference from the method but it will only redirect the iframe that the embedded Visualforce page is displayed in.
Ideally I'd like to refresh the top level window but I'm concerned there may be cross domain issues if the embedded visualforce page isn't in the same domain as the parent window.
As a basic test in I tried adding the following to the embedded Visualforce page:
<script>
window.setTimeout(testRedirect,2000);
function testRedirect() {
top.location.reload();
}
</script>
This resulted in Chrome logging the error:
Unsafe JavaScript attempt to access frame with URL https://na2.salesforce.com/006400000000000 from frame with URL https://ab2.na2.visual.force.com/servlet/servlet.Integration?lid=066400000000000&ic=1. Domains, protocols and ports must match.
So the domains differ for the Visualforce page.
It's a bit more code, but this works for me in all browsers, and I'm not getting any kind of cross-domain error.
Controller Extension:
public class Opp_Ext {
private ApexPages.StandardController stdController;
public String redirectUrl {public get; private set;}
public Boolean shouldRedirect {public get; private set;}
public Opp_Ext(ApexPages.StandardController stdController) {
this.stdController = stdController;
shouldRedirect = false;
}
public PageReference doStuffAndRedirect() {
shouldRedirect = true;
redirectUrl = stdController.view().getUrl();
return null;
}
}
VF Page:
<apex:page standardController="Opportunity" extensions="Opp_Ext" >
<apex:form >
<apex:commandButton value="Do Stuff" action="{!doStuffAndRedirect}" rerender="redirectPanel" />
<apex:outputPanel id="redirectPanel" >
<apex:outputText rendered="{!shouldRedirect}">
<script type="text/javascript">
window.top.location.href = '{!redirectUrl}';
</script>
</apex:outputText>
</apex:outputPanel>
</apex:form>
</apex:page>