2

How to get the path of js file in static resource.

for ex: /resource/1301987741012/resourceName/JSFileName.Js

what is this no. 1301987741012 .is it an Id of resource ?

4
  • My answer assumes you are trying to get this information from a Visualforce page. Is this what you need it for, or something else?
    – dphil
    Commented Apr 10, 2014 at 20:58
  • It helps give you a correct answer if you explain what you need the information for.
    – dphil
    Commented Apr 10, 2014 at 21:00
  • I am assigning the path of js file to a variable in js file
    – sfdc
    Commented Apr 10, 2014 at 21:06
  • If the logic originates from a Visualforce page, best to pass this value into your JavaScript from the Visualforce.
    – Keith C
    Commented Apr 10, 2014 at 21:12

4 Answers 4

5

In Visualforce, use

!URLFOR($Resource.myResource, 'js/JSFileName.js')

or whatever the resource name and path to the file you want is.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_resource.htm

3

This Delivering Static Resources with Visualforce article provides the background. The number is a timestamp.

Based on that you can write Apex code like this:

Map<String, String> m = new Map<String, String>();
for (StaticResource r : [
        select NamespacePrefix, Name, LastModifiedDate
        from StaticResource
        where Name in :names
        and CacheControl = 'Public'
        ]) {
    m.put(r.Name, 'resource/' + r.LastModifiedDate.getTime() + '/'
            + (r.NamespacePrefix != null ? r.NamespacePrefix + '__' : '') + r.Name);
 }
 return m;

to obtain the URLs (or just the number if that is what you want).

But the cleanest approach is obviously to use the built-in Visualforce mechanisms described in Referencing a Static Resource in Visualforce Markup to generate the URL including the timestamp for you.

1
  • 1
    Additionally, if you don't wish to use the 'cache busting' timestamp in the resource path it can be omitted. Doing so introduces the possibility that you may see an old resource retrieved from your browser's cache.
    – Mark Pond
    Commented Apr 10, 2014 at 22:09
1

Your problem is that of "referencing a static resource from another static resource". In this link , under the Static Resource Bundles section, you find useful info.

The number you mention is the static resource ID that salesforce provides for the static resource you upload. This ID changes for each run. You can see them under Sources tab in Chrome's developer console. (Refer these screen shots).
Run1: Static Resource id is 17067916541000

Run1: Static Resource id is 13019877741012

A workaround I figured is to add this line in my VFpage (MyStaticResource is the name of the Static Resource bundle that I've uploaded)

var resourceId='{!URLFOR($Resource.MyStaticResource)}';

Now I can access this var inside my javascripts (those that are bundled with the static resource itself).

For example: My Static resource looks like this:
MyStaticResource
   |
   |----img
   |       |--Jasmine.jpg
   |       |--Rose.jpg
   |
   |----scripts
   |       |--image_load.js

My Visualforce code is (
<apex:page >     <table>        <tr>          <td id="flower_page"></td>        <\tr>     <\table> </apex:page>

My Javascript code is ... $("#flower_page").html( "<div class=\"img-flower\"><img src=\""+resourceId+"/"+ Rose.jpg + "\" width=\"50\" height=\"50\" /></div>" ); ...

This would emit the following code. ... <table> <tr> <td id="flower_page"> <div class="img-flower"> <img src="/resource/1412387865123/MyStaticResource/img/Rose.jpg" width="50" height="50" /> </div> </td> ...

I hope this answers your question.

Let me know if you need more help on this.

Best

0
0

I think it is some sort of identifier for the resource. The following link will explain how to include javascript from a static resource:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_includeScript.htm

2
  • how to get the identifier for the resource
    – sfdc
    Commented Apr 10, 2014 at 20:55
  • What do you need it for?
    – dphil
    Commented Apr 10, 2014 at 20:55

You must log in to answer this question.

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