Iframe 嵌入在需要重定向到其他页面的详细信息页面中的Visualforce页面

Iframe 嵌入在需要重定向到其他页面的详细信息页面中的Visualforce页面,iframe,salesforce,Iframe,Salesforce,我有一个Visualforce页面,嵌入在Opportunities的详细页面上 页面中有一个命令按钮,用于调用后备控制器扩展中的方法 备份方法完成后,如何将用户重定向到其他页面? 我可以从该方法返回PageReference,但它只会重定向显示嵌入式Visualforce页面的iframe 理想情况下,我希望刷新顶级窗口,但我担心如果嵌入的visualforce页面与父窗口不在同一个域中,可能会出现跨域问题 作为中的一项基本测试,我尝试将以下内容添加到嵌入式Visualforce页面: &l

我有一个Visualforce页面,嵌入在Opportunities的详细页面上

页面中有一个命令按钮,用于调用后备控制器扩展中的方法

备份方法完成后,如何将用户重定向到其他页面?

我可以从该方法返回PageReference,但它只会重定向显示嵌入式Visualforce页面的iframe

理想情况下,我希望刷新顶级窗口,但我担心如果嵌入的visualforce页面与父窗口不在同一个域中,可能会出现跨域问题


作为中的一项基本测试,我尝试将以下内容添加到嵌入式Visualforce页面:

<script>
    window.setTimeout(testRedirect,2000);
    function testRedirect() {
        top.location.reload();
    }
</script>

setTimeout(testRedirect,2000);
函数testRedirect(){
top.location.reload();
}
这导致Chrome记录错误:

不安全的JavaScript试图使用URL访问帧 从带有URL的框架 . 域、协议和端口必须匹配

因此Visualforce页面的域不同。

尝试使用类

此外,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;
}

}

代码要多一点,但这在所有浏览器中都适用,而且我没有收到任何跨域错误

控制器扩展:

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='{!重定向URL}';

您需要使用
oncomplete=“window.top.location.href='{!'/'+obj.id}';“

我希望将此作为问题解决方案的一部分。可能我需要使用apex:actionFunction和一些javascript来在完成时更改
window.top.location.href
。嗨,Daniel,除非有人有一些超级诡计多端的策略,否则我想你会运气不佳-问题是嵌入的visualforce页面是从不同的域提供的,因此,您受到浏览器的XSS保护。希望有人能证明我错了,并找到一种方法让这一切为你服务!我本来是这么想的,但它只是在嵌入VF页面的迷你iframe中加载Opportunity页面布局。将commandLink/commandButton的目标设置为“_top”,它将在顶部框架(即整个窗口)中加载页面。太好了,谢谢。我想关键是允许跨域分配给window.top.location.href。然后,只需在按下按钮后呈现正确的Javascript即可。我确实得到了类似的结果,但你的更好,javascript只在按下按钮后发送到客户端。我回到这里,并提出了一个类似的解决方案,如果要重定向到的URL是已知的。我知道stackoverflow社区看不起这样的事情但是。。。非常感谢。很好。我想指出,如果您想在重新加载之前保存记录,只需在doStuffAndRedirect中的第一行添加一个“stdController.save();”。请提供更多关于如何使用以及在何处使用的说明。只使用代码的答案是不受欢迎的,例如,阅读答案的人无法快速准确地了解您的修复程序正在做什么或如何使用它。