1

I'm facing a little bit problem with <apex:include> tag.

Scenario: I have created a VF page which will executes javascript function after loading.

Eg:

<apex:page>
      <script>
$(document).ready(function(){
  //some code
  sampleMethod();
  // some code
});
</script>

<script>
function sampleMethod()
{
//some code
}

</apex:page>

This page is working fine. But, when i include it in another page using <apex:include> it is not executing sampleMethod(). Is there any limitation on <apex:include>?

2

apex:include resolves its reference before any other element on a page, early in the rendering process. This means that each apex:include will include only one page, and that reference must be non-null before any getters are called. This is different than most other elements, which are executed in page flow order. While this isn't documented, I did some extensive testing.

Test 1

<apex:page controller="xyz">
    <apex:repeat value="{!pageList}" var="aPage">
        <apex:include pageName="{!aPage}"/>
    </apex:repeat>
</apex:page>

This results in "aPage not found".

Test 2

<apex:page controller="xyz">
    <apex:repeat value="{!pageList}" var="aPage">
        <apex:variable name="pageRef" value="{!aPage}"/>
        <apex:include pageName="{!pageRef}"/>
    </apex:repeat>
</apex:page>

This results in "pageRef not found".

Test 3

<apex:component>
    <apex:attribute name="pageName" type="String" description="A page name."/>
    <apex:include pageName="{!pageName}"/>
</apex:component>

This results in "Cannot use apex:include in a component".

I tried a few other nonsensical arrangements, such as trying to use apex:param to call a method repeatedly, which mostly ended in "Page must be set to a non-null value".

I also consulted the documentation, thinking to try a apex:dynamicComponent, which actually explicitly states that a handful of elements have no Apex Code representation: all of them are of a similar logic: "we don't support these elements because they have to compile instead of resolving at run time."

Given that salesforce.com has explictly gone out of their way to block all legitimate attempts of using apex:include dynamically, we must conclude that they have explicitly blocked this functionality.

Finally, given salesforce.com documentation, I often say "If the system doesn't allow something, and the documentation doesn't clearly state that you can, it cannot be done."

Salesforce.com documentation is often positive, focusing on what the system can deliver, and goes out of its way to specifically exclude what the system cannot deliver. Generally, the only pages that describe limitations at all are those pages that speak of clearly defined limits, such as governor limits, API rate limiting, and the such. Any page that covers a feature that has some obscure limitation (such as apex:include not supporting non-static references) usually won't mention it. This is probably one of the most frustrating aspects of salesforce.com development.

  • Your answer doesn't appear to relate to the question which is about executing JavaScript in an included page. – Keith C Jul 15 '15 at 13:51
1

This page:

<apex:page>

Parent 1
<apex:include pageName="child"/>
Parent 2

<script src="https://code.jquery.com/jquery-2.1.4.min.js"/>
<script>
console.log('parent');
jQuery(document).ready(function() {
    console.log('parent ready');
});
</script>
</apex:page>

and its included page:

<apex:page>

Child

<script src="https://code.jquery.com/jquery-2.1.4.min.js"/>
<script>
console.log('child');
jQuery(document).ready(function() {
    console.log('child ready');
});
</script>
</apex:page>

outputs in the JavaScript console via the console.log calls:

child
parent
child ready
parent ready

so there appears to be no block to executing JavaScript in the included page.

  • Keith, thank u for sharing your answer. but,in my scenario control is entering into 'Child Ready' Loop but unable to execute a method – Rv1 Jul 15 '15 at 13:10
  • @Rv1 The ready isn't a loop but a single call to the supplied function that gets made when the page content has been fully parsed. You should scatter console.log calls into your code and look at your browser's console to find at what is going on. The example I've posted shows that the JavaScript can work in this scenario. – Keith C Jul 15 '15 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.