Working with URLs in JavaScript

The Liferay global JavaScript Object exposes methods, objects, and properties that access the portal context. Four of these are helpful when working with URLS: authToken, currentURL, currentURLEncoded, and PortletURL. If you have experience with Java development in Liferay DXP, you may have worked with some of these before. The Liferay global object is automatically available at runtime, so no additional dependencies are required.

This tutorial covers how to use the Liferay global JavaScript object to manipulate URLs. A complete list of the available methods and properties appears in the tables at the end of this tutorial.

Liferay PortletURL

The Liferay.PortletURL object provides methods for creating portlet API URLs (actionURL, renderURL, and resourceURL), through JavaScript. Below is an example configuration:

var portletURL = Liferay.PortletURL.createURL(themeDisplay.getURLControlPanel());

portletURL.setParameter('cmd', 'add_temp');
portletURL.setParameter('javax.portlet.action', '/document_library/upload_file_entry');
portletURL.setParameter('p_auth', Liferay.authToken);

See the Portlet URL Methods and Properties section for more information about the methods and properties used in the example above.

Liferay AuthToken

The Liferay.authToken property holds the current authentication token value as a String. The authToken is used to validate permissions when you make calls to services. To use the authToken in a URL, pass Liferay.authToken as the URL’s p_auth parameter, as shown in the example below:

portletURL.setParameter('p_auth', Liferay.authToken);

Liferay CurrentURL

The Liferay.currentURL property holds the path of the current URL from the server root.

For example, if checked from, the value is /es/web/guest/home, as shown below:

// Inside
console.log(Liferay.currentURL); // "/es/web/guest/home"

Liferay CurrentURLEncoded

The Liferay.currentURLEncoded property holds the path of the current URL, encoded in ASCII for safe transmission over the Internet, from the server root.

For example, if checked from, the value is %2Fes%2Fweb%2Fguest%2Fhome, as shown below:

// Inside
console.log(Liferay.currentURLEncoded); // "%2Fes%2Fweb%2Fguest%2Fhome"

Portlet URL Methods and Properties

Liferay.PortletURL Methods:

createURLbasePortletURL, paramsnew PortletURL(null, params, basePortletURL);
createActionURL new PortletURL(PortletURL.ACTION_PHASE);
createRenderURL new PortletURL(PortletURL.RENDER_PHASE);
createResourceURL new PortletURL(PortletURL.RESOURCE_PHASE);

Liferay.PortletURL Properties:


Once the portlet URL is created, you have access to several methods that you can use to manipulate the URL further:

setDoAsGroupIdSets the ID of the site, organization, or user group for the URLdoAsGroupIdThe updated Portlet URL Object
setDoAsUserIdSets the ID of the user to impersonatedoAsUserIdThe updated Portlet URL Object
setEscapeXMLSets whether the URL should be XML escapedtrue or falseThe updated Portlet URL Object
setLifecycleSets the portlet lifecycle of this URL’s target portletlifecycleThe updated Portlet URL Object
setNamesets the portlet URL’s javax.portlet.action namenameThe updated Portlet URL Object
setParameterCreates an individual parameter or replaces an existing reserved parameterkey,valueThe updated Portlet URL Object
setParametersCreates multiple parameters and/or replaces existing reserved parameters{key:value,…}The updated Portlet URL Object
setPlidSets the portlet layout IDplidThe updated Portlet URL Object
setPortletIdSets the ID of the target portletportletIdThe updated Portlet URL Object
setPortletModeSets the portlet mode, if the URL triggers a requestportletModeThe updated Portlet URL Object
setResourceIdSets the ID of the URL’s target resourceResourceIdThe updated Portlet URL Object
setSecureSets whether to make the URL secure (HTTPS).true or falseThe updated Portlet URL Object
setWindowStateSets the portlet’s window state, if the URL triggers a requestwindowStateThe updated Portlet URL Object
toStringReturns the URL as a String The portlet URL as a String
_isReservedParamReturns whether the parameter is reservedparamNametrue if the parameter is reserved

Now you know how to manipulate URLs using methods within the Liferay global JavaScript object.

Liferay DXP JavaScript Utilities

Liferay Theme Display

« Accessing ThemeDisplay InformationLiferay DXP JavaScript Utilities »
Was this article helpful?
0 out of 1 found this helpful