10

"{!$Setup.MyCustomSet__c.URL__c}";

I'm using the above to access custom setting on the page. But when I move the script to static resources and try to access it from there the value is not being fetched. Any help is much appreciated thanks!

1
  • Is it being rendered blank? Or does the script still show {!$Setup.MyCustomSet__c.URL__c}?
    – NSjonas
    Aug 6, 2015 at 22:04

2 Answers 2

13

Static resources are... static. There is no pre or post processing of their contents. Instead of putting globals from Visualforce code in your javascript, try putting some JavaScript in your Visualforce code.

vf-js-bridging-component

  1. Before your script loads, create some JavaScript bridging code:

    <script>
        $Setup = window.$Setup || {};
        $Setup.MyCustomSet__c = $Setup.MyCustomSet__c || {};
        $Setup.MyCustomSet__c.URL__c = '{!$Setup.MyCustomSet__c.URL__c}';
    </script>
    
  2. Now in your javascript (in a static resource) use the variables as if they were VF, without {!}

    function errorHandler() {
        console.log($Setup.MyCustomSet__c.URL__c);
        alert($Setup.MyCustomSet__c.URL__c);
    }
    

If the naming is consistent, your front-end team can pluck the values straight out of the JavaScript and nobody has to invent anything. Also has the benefit of masking variations due to namespace prefixes.

2
  • I'm not a great UI developer, Can you please explain what exactly does the code in the page mean/ does? I mean window.$setup = window.$setup || {} and the other 2 lines. Thanks!
    – cartman
    Aug 6, 2015 at 23:06
  • window.$Setup = window.$Setup || {} will "reference $Setup if it exists" or "initialize it". That way different page components can share stuff on $Setup and play nice together. @cartman Aug 6, 2015 at 23:28
6

You will need to pass this run-time value to your script as a parameter from the page. Dynamic values like this cannot be referenced directly in the Static Resource markup.

You've got quite a few options, here's a few:

  1. Pass the value as a parameter to a function.
    MyThing.init('{!$Setup.MyCustomSet__c.URL__c}');
  2. Set it as a member variable on an object that exists in the page.
    var MyThing = {}; MyThing.URL = '{!$Setup.MyCustomSet__c.URL__c}';
    Then in your script, you can reference MyThing.URL for the value

  3. Set it as a variable on the window object (or similar) and then have your Static Resource script reference it from there.

You must log in to answer this question.

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