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!

2
  • 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, 2013 at 15:08
  • Hey eyescream, yes I'm trying to do this via sidebar component. Would this make a difference?
    – McD
    May 11, 2013 at 15:15

2 Answers 2

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

5
  • Thanks Seb, would this still be the case if I were referencing something in the sidebar (custom HTML component)?
    – McD
    May 11, 2013 at 15:17
  • absolutely. just use /resource/<timestamp>/<name> instead of the {!$Resource.} expression
    – Seb Wagner
    May 11, 2013 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, 2013 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, 2013 at 2:03
  • When referencing from within static resources I omit the timestamp, ex /static-resource-name/background.jpg. May 22, 2014 at 2:09
0

Using a timestamp within your static resource path seems a bit problematic if that resource is constantly updated (like a stylesheet).

Following the official doc and looking at the generated meta tags in a page, I found that the following path works in my community app:

/customers/sfsites/c/resource/<static-resource-name>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .