Not able to use slingRequest.getURL() in sightly | Community
Skip to main content
cqsapientu69896
Level 4
August 27, 2016
Solved

Not able to use slingRequest.getURL() in sightly

  • August 27, 2016
  • 6 replies
  • 11271 views

I want to use the current page's browser url (not the page path) in sightly. I am not able to use location.href in server-side javascript as it is not supported.

http://stackoverflow.com/questions/28564059/window-console-object-not-accessible-in-aem-6-0-sightly-javascript

Previously in jsp we could use the request url after including global.jsp as

<%
    StringBuffer  test = slingRequest.getRequestURL();
%>
<c:set var="test"  value="<%=test%>"/>

It is mentioned that request object is implicitly available in sightly -- https://docs.adobe.com/docs/en/htl/docs/global-objects.html?wcmmode=disabled

But using ${request.URL} ,${request.getRequestURL} returns nothing. However; ${request.toString} returns org.apache.sling.scripting.core.impl.helper.OnDemandReaderRequest@25fbcebf object.

Right now I am using the url using serverside javascript as -- 

<div data-sly-use.browserUrl="pageUrl.js">
  <a href = "${browserUrl.url}">url</a>
</div>

In pageUrl.js --

"use strict";
use(function () {
    var browserUrl = {};    
    browserUrl.url = request.getRequestURL();
    return browserUrl;
});

 

Why is request object's getRequestURL not accessible in sightly while it is in jsp? And is there a cleaner way to include current page's url in sightly , without using serverside javascript ?

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 antoniom5495929

So you need to take also domain? 

I don't underestaind why you need this, also because if this is used as an internal link the domain is removed for the Linkstatechecker service.

But if you need to use for some reason you can create it in this way:

${request.requestURL.toString}  --> the best way

or just composing it:

 

<sly data-sly-test.scheme="${request.scheme}"/>
    <sly data-sly-test.servername="${request.serverName}"/>
    <sly data-sly-test.serverport="${request.serverPort}"/>
    <sly data-sly-test.val="${request.requestURI}"/>
    ${scheme}://${servername}:${serverport}${val}

It's not a bad way to get this url with a javascript model, but it's preferred to don't use it if you can take the information directly in sightly just only for performance reason.

If you are using a javascript model, this has to be translated in a java model and then used in sightly; if you are using directly sightly no translation needed.

Let me know if this can help you. 

6 replies

smacdonald2008
Level 10
August 29, 2016

Using JavaScript with HTL (Sightly) is a valid way. There are tasks - like using AJAX -with HTL where you need to use JS. As stated in the docs - some objects may not work - you can file a ticket too if you believe there is a bug in using a given Global object. 

joerghoh
Adobe Employee
Adobe Employee
August 29, 2016

Hi,

 you tried ${request.requestURL} already?

antoniom5495929
Level 7
August 29, 2016

You should use something like this:  

  <sly data-sly-test.val="${request.pathInfo}"/>

or this <sly data-sly-test.val="${request.requestURI}"/>

 

If you try to use requestUrl it doesn't works because the return type is a String Buffer that is a complex type instead of a string returned by the examples that i put before.

cqsapientu69896
Level 4
August 30, 2016

${request.requestURL} returns nothing

${request.pathInfo} returns the path of the page starting from content -- /content/a/b/c.html

${request.requestURI} returns the path of the page starting from content  /content/a/b/c.html

I needed the browser url; so I guess the best way would be the one that I am using -- using server side java script ????

antoniom5495929
antoniom5495929Accepted solution
Level 7
August 30, 2016

So you need to take also domain? 

I don't underestaind why you need this, also because if this is used as an internal link the domain is removed for the Linkstatechecker service.

But if you need to use for some reason you can create it in this way:

${request.requestURL.toString}  --> the best way

or just composing it:

 

<sly data-sly-test.scheme="${request.scheme}"/>
    <sly data-sly-test.servername="${request.serverName}"/>
    <sly data-sly-test.serverport="${request.serverPort}"/>
    <sly data-sly-test.val="${request.requestURI}"/>
    ${scheme}://${servername}:${serverport}${val}

It's not a bad way to get this url with a javascript model, but it's preferred to don't use it if you can take the information directly in sightly just only for performance reason.

If you are using a javascript model, this has to be translated in a java model and then used in sightly; if you are using directly sightly no translation needed.

Let me know if this can help you. 

cqsapientu69896
Level 4
September 9, 2016

Thanks, it helped.