Skip to main content

         This documentation site is for previous versions. Visit our new documentation site for current releases.      

Best practices for using custom JavaScript

Updated on December 13, 2022
Applicable to Theme Cosmos applications

By following best practices for using custom JavaScript, you ensure that you can fully benefit from such functionalities as custom error messages or client-side validation.

The following list includes guidelines that can help you ensure that custom JS runs as intended:

  • Test third-party script before deployment to avoid potential conflicts with internal script.
  • Verify that the browsers that you specify can display the customized code, because Pega Platform does not validate custom HTML code.
  • Pay attention to the order in which you put the scripts in the code, because the system runs the scripts sequentially.
  • Store the JavaScript code in its own separate JS file, because inline JavaScript inserted into a custom section or control requires more maintenance. Separate files also minify the JavaScript source code and enable JavaScript validation and linting.
  • For the highest quality and security, reduce the use of HTML fragment rules in application rulesets.
  • Attach global scripts to a top-level harness, or include them as part of a static bundle.
  • Load a local script for a particular custom section by using the <pega:onlyonce> directive.
  • If you use your custom control in multiple sections, load the JavaScript file during the initial loading of the portal by attaching it to the portal harness.
  • If the custom control is used infrequently, load the JavaScript file at the end of stream processing by using the <pega:static> JSP tag.

Have a question? Get answers now.

Visit the Support Center to ask questions, engage in discussions, share ideas, and help others.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best. is not optimized for Internet Explorer. For the optimal experience, please use:

Close Deprecation Notice
Contact us