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
asadim2asadim2 

How to reference Static Resources from JavaScript?

Hi,

 

I have uploaded some JS script and an image to Static Resources (zipped), then included it using the includescript tag in my page, and now I want to make references to some image in there  from within JavaScript. How can I do that? i.e. what path do I use for the image?

 

The VF guide states that includescript is translated to the following HTML:

 

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

 But what does that long number mean? I was thinking that maybe I could use the rendered HTML to reference the image thru JS.

 

Any ideas please? Thanks!

 

Message Edited by asadim2 on 02-01-2010 06:58 PM
bob_buzzardbob_buzzard

Here's an example from one of my orgs:

 

 

<img src="{!URLFOR($Resource.MyResources,'images/section_icons/sect-details.png')}"/

 

 In this case, MyResources is the name of the zip file, and the second parameter is the full path inside the zip file to the image.

 

asadimasadim

Thanks, though, I was looking for something slightly different :) I wanted to use the path for some file in Static Resources, say an image, in JavaScript (not in VF). So something like:

 

var path = path_to_image;

 

I may be able to use actionFunction to do this..

bob_buzzardbob_buzzard

In that case, wouldn't you just use the URLFOR, e.g.

 

var path="{!URLFOR($Resource.MyResources,'images/section_icons/sect-details.png')}";

 

 

elrenesonelreneson

Hi !

 

I've exactly the same problem and would like to know if there is any workaround ... I've seen some post about it and the solution using VF pages is given several times, but it doesn't answer what is asked.

 

I tried using a variable as bob_buzzard said, but isn't working either (inside onclick button javascript code).

 

Please, any help is appreciated

 

Thanks !!

XhitmanXhitman
Raghu_VirodhulaRaghu_Virodhula
Hi,

You can use JSENCODE to get the static resource path in javascript. For example if you want to retrieve the path of the image you stored in static resource you can use the below:

var img_path = '{!JSENCODE($Resource.testImage)}'