Find your content:

Search form

You are here

Static Resource

 
Share

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 ?


Attribution to: sfdc

Possible Suggestion/Solution #1

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


Attribution to: dphil

Possible Suggestion/Solution #2

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.


Attribution to: Keith C

Possible Suggestion/Solution #3

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


Attribution to: DavidSchach

Possible Suggestion/Solution #4

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


Attribution to: gvatreya
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32469

My Block Status

My Block Content