Parameters
filename |
string | The JavaScript to import from the xtk:javascript table. Use the format 'namespace:name' |
useCache |
boolean | Enables the cache. Enabled by default. |
Examples
loadLibrary is the import/export feature of the Adobe Campaign framework. It is used to enhance the reusability and testability of the code. It is comparable to the NodeJS import/require features.
Function call
Example of a Helper JavaScript file, with a function called by a Workflow and a JSSP:
JavaScript cus:helper in xtk:javascript:
/**
* Check that a string has a valid length.
* @param {string} str the string to check
* @return {boolean} true when the value is a non-empty string with a length of at most 255 characters
*/
function isLengthValid(str) {
return str && str.length <= 255;
}Called in a JavaScript activity of a workflow:
loadLibrary("cus:helper"); // isLengthValid()
if (!isLengthValid("[email protected]")) {
logError("not a valid email address");
}Or in a JSSP:
<%
loadLibrary("cus:helper"); // isLengthValid()
if (!isLengthValid("[email protected]")) {
document.write("not a valid email address");
}
%>When importing a JavaScript file, all its functions and variables are created in the global namespace. Prefer using the namespace pattern with
const MyClass = MyClass || {}; MyClass.myFunc = function(){}
Chained imports
Imports can happen in a function to limit it to the local scope:
// cus:helper
function isLengthValid(str) {
return str && str.length <= 255;
}
// cus:helperEnhanced
function isLengthValidEnhanced(str) {
loadLibrary("cus:helper"); // isLengthValid()
return str !== undefined && isLengthValid(str);
}Native framework call
Native functions can be used by importing the OOTB Adobe Campaign files, such as 📕 NL object with loadLibrary("/nl/core/shared/nl.js");:
/**
* Check that a value is a usable email address.
* @param {string} email value read from a queryDef result or a request parameter
* @return {boolean} true when the value is a non-empty string matching the email pattern
*/
function isEmailValidAdvanced(email) {
loadLibrary("/nl/core/shared/nl.js");
if (NL.isEmpty(email)) return false; // null, undefined or ""
if (!NL.isString(email)) return false; // a queryDef value can be an array
return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email); // contains @ and .
}Going further
- To import a library from NPM, such as Moment or Lodash, follow the Integrate an NPM library in Adobe Campaign article
See also
-
📕 queryDef- Helper to run a SQL SELECT query with JSON and returns an XML element. -
📕 DOMElement- Adobe Campaign's XML element: the type returned by queryDef and SOAP calls. Can be read and traversed. -
📕 NL.is- Adobe Campaign's type helpers to check XML values.