I'm developing a Managed package and just started to move some code, I noticed some of my javascript functions are exposed in the code, I would like to move all that code to static resources leaving a clean page just doing the instance to the resource.

What is the best practice on this?, how can I call my functions from my visualforce?


2 Answers 2


Well there is no specific best practice defined by SFDC for moving inline JS code into file and accessing that using static resource.

But, you can take a call based on your needs.

Lets first see how to move your JS code in static resource.

1) Move all js code(mainly functions) in separate JS file.
2) Upload this file as static resource
3) include this file in your VF page using syntax :

<apex:includeScript value="{!$Resource.MyJavascriptFile}"/>

More details at : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_resources.htm

1) Your code becomes more modular
2) You can reuse your code
3) Browser can cache JS file and improve performance of page

When to avoid?
1) If your code is very specific to page then it make more sense to keep it inline.
2) Its easy to make changes in inline code


I guess I found a quick solution after been looking for some examples.

1.Create a new .js file (it could be created with Sublime Text or any other text editor).

  1. In your file you can add all functions as you were working in any other kind of project.

  2. in the function you want to call the Remoting function you need to change the way you call the remoting method from visualforce with this format:

yournamespace.yourapexcontroller.themethod(function(result,event){ //here all your actions after validate event.status and use the results });

  1. Upload your file as static resource
  2. invoke your script with

Here an example of how my js file looks like:

enter image description here

and how the visualforce looks like:

enter image description here

and here some console.log();

enter image description here

Finally I minimized the javascript code and all still worked.

Hope it helps.

You must log in to answer this question.

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