I am using static resource, file default.js, where all javascript magic for visualforce page happens. But, I come to the problem, when i want to use custom labels, there is an error

$Label is not defined

Syntax i am using is $Label.Name_of_the_label... even Illuminated Cloud is showing me the name of the label is correct and everything but label is not there and console showing me error.

//Add below 2 lines of code in the visualforce page
window.$Label = window.$Label || {};
$Label.customLabel= '{!($Label.myLabel)}'; // assigning the label to the JS variable.
var customLabelval =  $Label.customLabel;

Expressions are not evaluated in a Static Resource. They are used verbatim. The standard way to handle this is to have a global variable in your main Visualforce page that handles this:

var labels = { ABC: "{!JSENCODE($Label.ABC)}", XYZ: "{!JSENCODE($Label.XYZ)}" };


var someString = labels.ABC;

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.