How to add custom label in static resource JS alert , i want to create a alert error message using a custom label in static resources
-
I believe this is what you are looking for- salesforce.stackexchange.com/questions/32746/…C0DEPirate– C0DEPirate2016-05-06 07:03:56 +00:00Commented May 6, 2016 at 7:03
-
Custom label is not stored in static resources. Custom label is a different metadata type which can be accessed in Apex/Visualforce/Javascript directly.ajinkyah– ajinkyah2016-05-06 07:09:12 +00:00Commented May 6, 2016 at 7:09
-
but unable to do for alertsudh– sudh2016-05-06 07:10:13 +00:00Commented May 6, 2016 at 7:10
-
Please share your codeajinkyah– ajinkyah2016-05-06 07:12:32 +00:00Commented May 6, 2016 at 7:12
Add a comment
|
1 Answer
If we need to access labels in JS file which is saved in static resource: There are two way to do the same requiremnt:
Pass label as a parameter or pass-through attributes to JS function.
JavaScript bridging component:
2.a) loads before your script, like so:
<script>
window.$Label = window.$Label || {};
$Label.MyError = '{!JSENCODE($Label.MyError)}';
$Label.MyPrompt = '{!JSENCODE($Label.MyPrompt)}';
$Label.MyMessage = '{!JSENCODE($Label.MyMessage)}';
</script>
2.b) Now in your javascript (in a static resource) use the variables just as if they were VF without {!}
function errorHandler() {
console.log($Label.MyError);
alert($Label.MyMessage);
}
-
filesToUpload.length = 0; console.log('inside'); _fadeOutFileUploadPopUp(); $j("#closeButton").click; alert($Label.Easy_Archive_Storage_Space_Error); } } else { console.log(event.status);sudh– sudh2016-05-06 07:15:51 +00:00Commented May 6, 2016 at 7:15
-
@sudh When you add javascript in static resource you cannot access Custom Labels directly. As in the answer, you need to first store it in variables/constants and then JS in static resource can access these variables/constants.ajinkyah– ajinkyah2016-05-06 07:18:40 +00:00Commented May 6, 2016 at 7:18