how to get currentPage object in javascript, as javascript file resides in clientlibs folder of the component. please give me example code snippets. | Community
Skip to main content
New Participant
March 24, 2021
Solved

how to get currentPage object in javascript, as javascript file resides in clientlibs folder of the component. please give me example code snippets.

  • March 24, 2021
  • 3 replies
  • 3310 views
No text available
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @karthick1356 

When you say you want currentPage object in JS which is inside your clientlib, for what purpose you need it? You will not be able to get the currentPage object in JS as the context will not be avilable on clientside and it's either available on HTL or Java which is on server side.


If you can provide the use case we can come with a concrete answer.

 

Thanks!

3 replies

Kiran_Vedantam
New Participant
March 24, 2021

Hi @karthick1356,

 

you can use Javascript USE API to get all the values you need.

 

Sample code:

use(function () {
    var Constants = {
        DESCRIPTION_PROP: "jcr:description",
        DESCRIPTION_LENGTH: 50
    };

    var title = currentPage.getNavigationTitle() || currentPage.getTitle() || currentPage.getName();
    var description = properties.get(Constants.DESCRIPTION_PROP, "").substr(0, Constants.DESCRIPTION_LENGTH);

    return {
        title: title,
        description: description
    };
});

Reference page: https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/use-api-javascript.html?lang=en#dependencies

 

Hope this helps.

 

Thanks,

Kiran Vedantam

Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
March 24, 2021

Hi @karthick1356 

When you say you want currentPage object in JS which is inside your clientlib, for what purpose you need it? You will not be able to get the currentPage object in JS as the context will not be avilable on clientside and it's either available on HTL or Java which is on server side.


If you can provide the use case we can come with a concrete answer.

 

Thanks!

New Participant
March 24, 2021
I was trying to get language of the page. is there any possible way to get language in the frontend?
Bhuwan_B
New Participant
March 24, 2021