How to access a URL inside g:evaluate tag in Jelly

antonymirza
Tera Expert

Developing a new site using ServiceNow CMS and need to access the URL value inside jelly code. I see that window.location method does not work inside j or g tag in jelly instead in script tag.

 

Can someone please explain how to access URL value inside jelly tags?

1 ACCEPTED SOLUTION

This piece of code seems to work fine on a demo instance:



<g:evaluate var="jvar_site_and_page" jelly="true">
      var page = new GlideRecord('content_page');
      page.get('${RP.getParameterValue('sysparm_sys_id')}');
      page.content_site.url_suffix + '/' + page.url_suffix;
</g:evaluate>


Current URL is this:<br/>
${RP.getParameterValue('jvar_base_uri')}${jvar_site_and_page}.do


View solution in original post

12 REPLIES 12

That's because getReferringURL() returns the link from which you came to the current page, not the URL of the current page itself.



Why do you need to get the full URL? You know the name of the page you are working on. So you can use it in the code without reading the URL. It is usually parameters that need to be retrieved dynamically because you never know their values beforehand. And to access parameters you can use the following methods:



RP.getParameterValue(parameterName)


RP.getParameters()



In fact, I think you should be able to get the URL of the current page as follows:



RP.getParameterValue('sysparm_this_url')


Tested in a demo instance and unfortunately it does not seem to work in content pages.



But you can still get the sys_id of your content page using RP.getParameterValue('sysparm_sys_id') and then query Pages [content_page] table to get the URL suffix of the page and the corresponding content site.


This piece of code seems to work fine on a demo instance:



<g:evaluate var="jvar_site_and_page" jelly="true">
      var page = new GlideRecord('content_page');
      page.get('${RP.getParameterValue('sysparm_sys_id')}');
      page.content_site.url_suffix + '/' + page.url_suffix;
</g:evaluate>


Current URL is this:<br/>
${RP.getParameterValue('jvar_base_uri')}${jvar_site_and_page}.do


Hi Brad and Slava, Thank you for your kind support. It was a great assistance and I am able to build my solution.


Brgds, AM


I know this is a while ago (and that this might be considered picky!), but best practice is to use the 'jelly' variable when doing this, and not to use JEXL expressions inside an evaluate.



You've already done 'jelly=true' when initializing the g:evalauate tag, so you have access to your parameter with the following:


jelly.RP.getParameterValue('sysparm_sys_id');