function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
john hejohn he 

Import Javascript in the Visualforce page

we have two approaches to import/refer the javascript in the visualforce page:

 

approach 1:

<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>

 

approach 2

<script src="https://cs5.salesforce.com/resource/1331877511000/MyJavascriptFile"></script>

 

the appoach 1 is using the OOTB Visalforce component, and it is recommanded by SFDC

the approach 2 has the hard code issue when migrating the vf page from one ENV to another

 

the approach 1 put the javascript reference in the HTML head

the approach 2 put the javascript reference in the HTML body

 

Beside the above two differencies, are there any other differencies between these two approaches when import js in VF page?

 

the reason I asked this question is that I create a vf page and refer the js via approach 2.   but when I am trying to using approach 1 to fix the hard code issue, the js does not work as expected.....

 

thanks!

 

 

 

Alessandro RibasAlessandro Ribas

I solved this issue in this way

 

<script src="{!$Resource.MyJavascriptFile}" type="text/javascript"  ></script>

 

Don't forget to use the type of script ---- type="text/javascript"

 

I hope I have helped.