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
sreejasreeja 

Adding Javascript in salesforce

 Hi , i had added the alert.js file in saticresource as alert.

alert.js:-
<script>
function myFunction() {
  alert("I am an alert box!");
}
</script>



 and in the Visualforce page :

 
<apex:page >
  
      <apex:includeScript value="{!$Resource.alert}"/>
     
  
   <apex:includeScript value="{!URLFOR($Resource.alert, 'alert')}"/>
  
  
  
  
  <apex:stylesheet value="{!URLFOR($Resource.alert, 'alert')}"/>
  
  <button onclick="myFunction()">Try it</button>
</apex:page>


functionality is not getting work .. thanks 
 
Meghna Vijay 7Meghna Vijay 7
Hi,

Remove <apex:includeScript value="{!URLFOR($Resource.alert, 'alert')}"/> and <apex:stylesheet value="{!URLFOR($Resource.alert, 'alert')}"/> .
 
Deepali KulshresthaDeepali Kulshrestha
Hi,
Please refer to these links it may be helpful to you: 
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_includeScript.htm
https://developer.salesforce.com/forums/?id=906F0000000BXFQIA4
https://salesforce.stackexchange.com/questions/132042/display-an-alert-on-the-visualforce-page-from-controller

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha 
sreejasreeja
hi  all , thanks for your reply , the Scenario is : i had created a visualforce page with a button :
<apex:page >
  
  
  <button onclick="myFunction()">Try it</button>
</apex:page>

i have an alert.js file , i had added it to static resource and saved as  alert.  i need to call the staticresource  in visualforce page, for acheiveing the button onclick functionality .

 thanks..
Deepali KulshresthaDeepali Kulshrestha
Hi,
You can call your static resource as follows:
<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>

Please refer to the following link as they may be helpful:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_resources_reference.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_resources_iframe.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.


Thanks and Regards,
Deepali Kulshrestha