Expand my Community achievements bar.

SOLVED

Externalizing URL in JavaScript Use Api

Avatar

Level 3

Hi All,

How can I use Externalizer in JavaScript Use API, I can easily use it using Java API. But I need this in JavaScript Use API to add canonical tag in head section of pages for SEO purpose.

-Thanks,
Raghava

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi, Raghava.

You can use the sling global object (it is an instance of the SlingScriptHelper class) to obtain a reference to the Externalizer service.

Untested code:

var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer); var externalURL = externalizer.externalLink(resolver, resource.getPath());

Regards,

Daniel.

View solution in original post

5 Replies

Avatar

Correct answer by
Level 4

Hi, Raghava.

You can use the sling global object (it is an instance of the SlingScriptHelper class) to obtain a reference to the Externalizer service.

Untested code:

var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer); var externalURL = externalizer.externalLink(resolver, resource.getPath());

Regards,

Daniel.

Avatar

Level 3

Hi Daniel,

Thanks for the inputs, I'll try implementing this. 

Avatar

Level 4

Hi, Raghava

Here is the example with CQ JS API - CQ.HTTP.externalize. You may try something like this.

 var currentPageContent = CQ.HTTP.get(CQ.HTTP.externalize(CQ.utils.WCM.getPagePath() + "/jcr:content.json"));

Thanks! 

Avatar

Level 4

Vamsi, AFAIK CQ.HTTP is exposed by a client lib and therefore is not available to be used in a JavaScript Use (a server side element interpreted by Rhino).

Regards,

Daniel.

Avatar

Level 3

Thanks Daniel it worked, 

Working code snip: 

            var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer);
            var externalURL = externalizer.externalLink(global.resource.getResourceResolver(),"publish", global.currentPage.getPath());

 

Thanks.