Loading
设置和维护 Salesforce 组织
替换 window.location 方法

替换 window.location 方法

window.location 变量是用于将浏览器重定向到新页面的 JavaScript 方法。Visualforce 页面中直接设置 window.location 的 JavaScript 代码不兼容 Lightning Experience。这不是好消息。好消息是 sforce.one 导航方法的功能相同,并且兼容 Lightning Experience。

所需的 Edition

适用于:GroupProfessionalEnterprisePerformanceUnlimitedDeveloper Edition
所需用户权限
创建、编辑或删除 Visualforce 页面: 自定义应用程序

由于 window.location 可以通过多种方式使用,但无法解决所有可能性。让我们了解一个 window.location 用作客户编辑页面链接的情况。然后,您可将此示例(包含代码示例)用作解决页面中其他 window.location 事件的指南。如果您可以确定 window.location 方法尝试执行的操作,则很有可能是因为存在 sforce.one 解决方法。

  1. 在 Visualforce 代码中查找 window.location。
    1. 从“设置”中,在快速查找框中输入 Visualforce,然后选择 Visualforce 页面
    2. 单击 Visualforce 页面旁边的编辑
    3. 搜索以下对象的 Visualforce 标记:window.location
      <apex:page standardController="Account" lightningStylesheets="true">   
        <apex:form >      
          <apex:pageBlock >
            <apex:pageBlockSection title="Edit">
              <apex:commandButton value="Edit" 
               onclick="window.location='/{!Account.Id}/e'; return false;"/>
            </apex:pageBlockSection>
          </apex:pageBlock>
        </apex:form> 
        <apex:detail />
      </apex:page>
  2. 确定 window.location 的重定向对象。通过在 Salesforce Classic 中测试 Visualforce 页面,您可以查明情况。
  3. 查找适当 sforce.one 方法,以替换 window.location。
  4. 使用 sforce.one 导航,替换 window.location 的所有实例。
    <apex:page standardController="Account" lightningStylesheets="true">   
      <apex:form >    
        <apex:pageBlock >
          <apex:pageBlockSection title="Edit">
            <apex:commandButton value="Edit" 
             onclick="sforce.one.editRecord('{!Account.Id}');"/>
          </apex:pageBlockSection>
        </apex:pageBlock>
      </apex:form>    
      <apex:detail />
    </apex:page>
  5. 保存更改。
  6. 如果在 Salesforce Classic 和 Lightning Experience 中访问 Visualforce 页面,请使两个选项可用。使用 if 语句,以对代码进行分支。
    <apex:page standardController="Account" lightningStylesheets="true">  
       <script>
         function isLightningExperience(){
            if (UITheme.getUITheme() === 'Theme4d' || UITheme.getUITheme() === 'Theme4u'){
                sforce.one.editRecord('{!Account.Id}');
            } else {
                window.location='/{!Account.Id}/e';
            }
        }
       </script>     
       <apex:form >    
         <apex:pageBlock >
           <apex:pageBlockSection title="Edit">
             <apex:commandButton value="Edit" 
              onclick="javascript:isLightningExperience();return false;"/>
           </apex:pageBlockSection>
         </apex:pageBlock>
       </apex:form>    
       <apex:detail />       
    </apex:page>
 
正在加载
Salesforce Help | Article