"{!$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!
"{!$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!
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.
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>
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.
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
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:
MyThing.init('{!$Setup.MyCustomSet__c.URL__c}');
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
Set it as a variable on the window
object (or similar) and then have your Static Resource script reference it from there.