Visualforce页面嵌入在需要重定向到其他页面的详细信息页面中
我有一个嵌入到Opportunities详细信息页面的Visualforce页面。Visualforce页面嵌入在需要重定向到其他页面的详细信息页面中
页面内有一个命令按钮,用于调用后备控制器扩展中的方法。
一旦支持方法完成,我该如何将用户重定向到另一个页面?
我可以从该方法返回一个PageReference但它只会重定向嵌入Visualforce页面显示在iframe。
理想情况下,我想刷新顶层窗口,但我很担心有如果嵌入式visualforce页面与父窗口不在同一个域中,则可能会出现跨域问题。
正如我尝试添加下列到嵌入式Visualforce页面的基本测试:
<script>
window.setTimeout(testRedirect,2000);
function testRedirect() {
top.location.reload();
}
</script>
这导致在Chrome中记录错误:
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.
所以域的不同Visualforce页面。
这有点更多的代码,但是这对我的作品在所有浏览器,我没有得到任何类型的跨域错误。
控制器扩展:
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页:
<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>
太好了,谢谢。我想关键的一点是,跨域允许分配给window.top.location.href。然后这只是在按下按钮之后呈现正确的Javascript的问题。我最终得到了类似于此的东西,但是在按下按钮之后,只有JavaScript才会发送到客户端,这样您的效果会更好。 – 2012-07-20 00:32:52
我已经回过头来,并提出了一个类似的解决方案,如果重定向到的URL是事先知道的。请参见[嵌入式Visualforce页面页面变为内嵌](http://salesforce.stackexchange.com/questions/11158/pagereference-from-embedded-visualforce-page-becomes) – 2013-04-30 02:13:36
太棒了!谢谢:) – 2014-09-10 10:23:54
尝试使用PageReference类
此外setRedirect将是有益的
样品:
public class mySecondController {
Account account;
public Account getAccount() {
if(account == null) account = new Account();
return account;
}
public PageReference save() {
// Add the account to the database.
insert account;
// Send the user to the detail page for the new account.
PageReference acctPage = new ApexPages.StandardController(account).view();
acctPage.setRedirect(true);
return acctPage;
}
}
我原本以为是这样,但它只是在VF页面嵌入的迷你iframe中加载商机页面布局。 – JCD 2012-07-19 12:42:17
将commandLink/commandButton的目标设置为“_top”,并且它会将页面加载到顶部帧,即完整的窗口。 – 2014-07-25 10:01:02
您需要使用oncomplete = "window.top.location.href = '{!'/'+obj.id}';"
请提供更多解释如何以及在哪里使用。只有代码才会回答不满,例如,通过阅读答案的人无法快速计算出您的修补程序在做什么或如何使用它。 – clearlight 2017-01-05 09:55:51
我期待以此作为我的[CSRF安全定制解决方案的一部分按钮链接到Apex方法](http://stackoverflow.com/questions/10809520/csrf-safe-custom-button-linked-to-apex-method/11006021#11006021)的问题。这可能是我需要使用apex:actionFunction与一些JavaScript来完成改变'window.top.location.href'。 – 2012-07-19 00:58:12
嗨,丹尼尔,除非有人有一些超级偷偷摸摸的策略,我认为你会因此失去运气 - 问题在于,嵌入式视觉页面页面是从不同的域提供的,所以你需要受到浏览器的XSS保护。希望有人证明我错了,并找到一种方法来为你做这个工作! – 2012-07-19 11:06:15