Javascript Code Standards for Liferay

Created: 13 April 2020  Modified:

This article is meant to provide guidance for myself and others on what I would recommend from my coding experience using Liferay. Most of this experience is carried forward from Liferay 6.2 but looks to work well with Liferay 7.1. We continue to maintain backward compatibility with IE11.

Coding Standards for Liferay

Our standards will be based on what has worked well for us. Javascript: the Good Parts is a good resource for the many cases not addressed by this document. Douglas Crockford also has some fantastic lectures on Youtube. The one covering the history of programming focusing on Javascript is my favorite. JavaScript - Volume 1: The Early Years.

Favor putting Javascript in JSPs

Favor placing Javascript and CSS in JSP files and including the files in other JSP files.

Always Namespace

Whether placing your Javascript in a JSP or in a JS file it should always be namespaced. We have been quite lax in namespacing in our JS files.

JSP Example

<script> 
function <portlet:namespace/>displayErrorAlert(messageInput) {
  <portlet:namespace/>addAlerts(messageInput, 'ALERT_ERROR');
}
<script> 

JS Example

<script> 
var myObject = {
  getSomething: function (options) {
  //Do Stuff
  }
}
<script> 
tags: liferay - javascript - code - standard - code standard
   Less Is More