【问题标题】:Cannot access javascript function in ADF InlineFrame无法访问 ADF InlineFrame 中的 javascript 函数
【发布时间】:2019-07-16 13:56:50
【问题描述】:

我有一个包含 af:InlineFrame 的 jsff 页面。

这个 InlineFrame 的来源是 HTML 文件,比如Frame.html

此 html 文件有一个名为 inlineframeFunction() 的 javascript 函数

我在jsff 页面中添加了一个按钮。

我的用例是在单击我无法实现的按钮时调用函数 inlineframeFunction。

var doc = inlineFrame.contentDocument? 
inlineFrame.contentDocument: inlineFrame.contentWindow.document;
doc.frameFunction();

Frame.html

<script type="javascript">    
    function inlineframeFunction(){
        alert('Inline Frame Function ');
    }
  </script>

JSFF 页面

<af:panelGroupLayout id="pgl1" layout="vertical">

      <af:resource type="javascript">

        function inlineFrameRegionPageFunction() {
          alert('Region Page Function');
          var inlineFrame = document.getElementById('r1:0:if2');
          var doc = inlineFrame.contentDocument? inlineFrame.contentDocument: inlineFrame.contentWindow.document;
          doc.frameFunction();
        }
      </af:resource>
    </af:panelGroupLayout>
    <af:panelGroupLayout id="pgl2" layout="vertical">
      <af:panelBox text="Inline Frame Region" id="pb2"
                   inlineStyle="background-color:Lime; border-color:Lime;">
        <f:facet name="toolbar"/>
        <af:inlineFrame id="if2" source="/Frame.html" shortDesc="InlineFrame"
                        inlineStyle="background-color:Gray;"/>
        <af:commandButton text="Inline Region Button" id="rb2"
                          actionListener="#{pageFlowScope.RegionBean.onClickInlineFrameRgnButton}"/>
      </af:panelBox>
    </af:panelGroupLayout>

【问题讨论】:

  • 是关于actionListener Java 调用你的js“inlineframeFunction”的问题吗?
  • 是的,你是对的

标签: javascript oracle-adf


【解决方案1】:

我使用了以下方法并且成功了!

    function inlineFrameRegionPageFunction() {          
       var frameComp =$('[id*="InlineFrame"]', document)[0].id;
    document.getElementById(frameComp).contentWindow.frameFunction();
    }
  </af:resource>

【讨论】:

    【解决方案2】:

    要从 adf 中的托管 bean 执行 javascript,您可以使用以下函数 (https://cedricleruth.com/how-to-execute-client-javascript-in-an-adf-java-bean-action/):

    /*** In YOURJSF.jsf button, or other component that need to execute a javascript on action, add : ****/
    <af:commandButton text="ClickMe" id="cb1" actionListener="#{YOURSCOPE.YOURJAVABEAN.clickToExecuteJavascriptAction}"/>
    
     /*** In YOURJAVABEAN.java class add : ***/
     public void clickToExecuteJavascriptAction(ActionEvent actionEvent) {
      this.executeClientJavascript("console.log('You just clicked : " + actionEvent.getSource() + " ')");
      //Note: if you use a java string value in this function you should escape it to avoid breaking the javascript.
      //Like this : stringValue.replaceAll("[^\\p{L}\\p{Z}]", " ")
     }
    
    //You should put this function in a util java class if you want to use it in multiple bean
    public static void executeClientJavascript(String script) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExtendedRenderKitService service = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
     service.addScript(facesContext, script);
    }
    

    那么在你的情况下,参考这个问题在你的动作监听器中使用javascript调用你的iframe js函数(Calling javascript function in iframe

    document.getElementById("if2").contentWindow.inlineframeFunction();
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2011-05-02
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多