Saturday, 30 September 2017

VisualForce - loadOnReady attribute on apex:includeScript

Arpit Sharma


Salesforce provide us apex:includeScript tag to include script on visual force page.


In this post we will discuss about loadOnReady attribute of apex:includeScript tag.

Role of loadOnReady attribute in apex:includeScript - 

1. loadOnReady attribute have two value i.e false and true.

2. Default value of this attribute is false.

3. If value of this attribute is false then script load immediately.

4. If value of this attribute is true then script will load when page is ready.


Let us take an example to understand loadOnReady attribute

1. When loadOnReady attribute is false then we get angular at both place outside of onload and inside the on onload.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<apex:page >
  <!-- External Scripts -->
      <apex:includeScript loadOnReady="false" value="{!$Resource.angular_js_script}" />
  <!-- External Scripts -->
  <script>
      alert(typeof angular);//we will get angular here.
      window.onload = function(){
          alert(typeof angular);// we will get angular here.
      }
  </script>
  
</apex:page>

2. When loadOnReady attribute is true then we get angular only in inside the on load.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<apex:page >
  <!-- External Scripts -->
      <apex:includeScript loadOnReady="false" value="{!$Resource.angular_js_script}" />
  <!-- External Scripts -->
  <script>
      alert(typeof angular);//we will get undefined here means angular is not loaded here.
      window.onload = function(){
          alert(typeof angular);// we will get angular here.
      }
  </script>
  
</apex:page>

ReferencesSalesforce docs

About the Author

Arpit Sharma / Author & Editor

Certified Salesforce Developer | Salesforce Developer at Cognizant | Blogger