What's the best practice of including javascritps in salesforce? I want to minified and concat all js files used in our app. When we are developing we want to use js files as separte files but on production environment we want to have a single file.


You can write a query in your controller

public boolean runningInASandbox {get { return [SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox;} }

You can then use this boolean to select what static resource get to play

    <apex:outputPanel rendered="{!not(runningInASandbox)}" layout="none">
    //prod javascript reference
    <apex:outputPanel rendered="{!runningInASandbox}" layout="none">
    //sandbox javascript reference
  • I don't set up my dev environment in sandbox but I should set up in sandbox and that should work.
    – user3211
    Aug 11 '15 at 3:54

Use a Static Resource.

A static resource can be an archive file (.zip etc) so you can bundle your separate Javascript files inside it.

Ant can help you Zip Stuff.

You could use requirejs to handle the loading of your Javascript modules so that your Javascript references will work irrespective of file structure. Cary Landholt's videos explain those concepts well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.