+ Start a Discussion
Raj R.Raj R. 

How to include javascript in visualforce?

Hi,

I am creating a visualforce page where I am trying to include two javascript files, but they are not running when the VF page loads. What is the correct way to include javascript in VF?

I have tried (with no luck):
  1. <script src="{!$Resource.jquery}"></script>
  2. <apex:includeScript value="{!URLFOR($Resource.jquery, 'jquery.min.js')}"/>
Best Answer chosen by Raj R.
CongnizentCongnizent
Hi rRup ,
I tried it on my system and I think you way to refering javascript is correct . Please take care of name of static resource like you created a new static resource named 'jquery' having file 'jquery.min.js' . So you have to use static resource name like <script src="{!$Resource.jquery}"></script> . Or by include script like <apex:includeScript value="{!$Resource.jquery}"/>  . If it is correct then jquery is included , It might override by any other jquery .

So please let me know if it helps or need further elebroation .

Thank you 

All Answers

CongnizentCongnizent
Hi rRup ,
I tried it on my system and I think you way to refering javascript is correct . Please take care of name of static resource like you created a new static resource named 'jquery' having file 'jquery.min.js' . So you have to use static resource name like <script src="{!$Resource.jquery}"></script> . Or by include script like <apex:includeScript value="{!$Resource.jquery}"/>  . If it is correct then jquery is included , It might override by any other jquery .

So please let me know if it helps or need further elebroation .

Thank you 
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi,

Please use the below Syntax :
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>
//MyJavascriptFile is name of your static resource in which JS file is stored.

Abhishek
 
Dushyant SonwarDushyant Sonwar
Hi rRup,

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

use urlFor if you have uploaded a zip file .
Hope this helps.
Bushra SalmanBushra Salman
https://forcefixes.blogspot.com/2017/12/writing-and-calling-java-function-in.html

this might help