3

I'd like to reference a Javascript file (Static Resource) in an HTML area within Salesforce - is this possible? Or can I only reference Static Resources from a VisualForce page?

If it is possible, would I be correct in using this code to do so: <apex:includeScript value="{!$Resource.MyJavascriptFile}"/>

Thanks in advance!

  • What do you mean by "HTML area"? S-Controls? HTML sidebar component? I think sidebar components don't parse dollar-variables ($User, $Resource etc) nicely. – eyescream May 11 '13 at 15:08
  • Hey eyescream, yes I'm trying to do this via sidebar component. Would this make a difference? – McD May 11 '13 at 15:15
5

it is possible.

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

translates to

<script type='text/javascript' src='/resource/1233160164000/MyJavascriptFile'>

or more generally speaking the path will be translated to

/resource/<timestamp>/<name>

which means that you can reference your static resource without the formulas

For more details see the

Component reference: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_includeScript.htm
and the Developer Wiki: http://wiki.developerforce.com/page/Delivering_Static_Resources_with_Visualforce

  • Thanks Seb, would this still be the case if I were referencing something in the sidebar (custom HTML component)? – McD May 11 '13 at 15:17
  • absolutely. just use /resource/<timestamp>/<name> instead of the {!$Resource.} expression – Seb Wagner May 11 '13 at 15:21
  • 1
    Cut the timestamp part off :D Less maintenance effort when eventually you have to upload new version & timestamp changes. – eyescream May 11 '13 at 15:25
  • ...unless you want your end-users to not use cached older versions of the resource, which is why the timestamp is there. It's a bit more maint when you change it, but you'll avoid having to tell users to ctrl-F5 to force a reload of the cached resource. – jkraybill May 13 '13 at 2:03
  • When referencing from within static resources I omit the timestamp, ex /static-resource-name/background.jpg. – Daniel Sokolowski May 22 '14 at 2:09

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.