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.
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 ?
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Hi,
you tried ${request.requestURL} already?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
${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 ????
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Thanks, it helped.
Views
Replies
Total Likes
Views
Likes
Replies