Sightly Javascript | Community
Skip to main content
Level 2
May 11, 2018
Solved

Sightly Javascript

  • May 11, 2018
  • 4 replies
  • 1574 views

Hello,

We have a requirement where authors should be able to input javascript code from the dialog and it should render as is within HTL.

We provided a textarea for the same and in sightly have used ${properties.javascriptcode@ context='scriptString'} to render

This however upon render shows output similar to the following -

<script>\r\nfunction myFunction() {\r\n var str = \x22The best things in life are free\x22;\r\n var patt = new RegExp(\x22e\x22);\r\n var res = patt.test(str);\r\n document.getElementById(\x22demo\x22).innerHTML = res;\r\n}\r\n<\/script>

Is there a way to unescape these characters for scripts to render properly.Looking forward to your response.

Thanks.

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 kautuk_sahni

You can use JavaScript function unescape() for this.

Str="\r\nfunction myFunction() {\r\n var str = \x22The best things in life are free\x22;\r\n var patt = new RegExp(\x22e\x22);\r\n var res = patt.test(str);\r\n document.getElementById(\x22demo\x22).innerHTML = res;\r\n}\r\n"

     document.write(unescape(str));

Output:- function myFunction() { var str = "The best things in life are free"; var patt = new RegExp("e"); var res = patt.test(str); document.getElementById("demo").innerHTML = res; }

4 replies

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
May 11, 2018

You can use JavaScript function unescape() for this.

Str="\r\nfunction myFunction() {\r\n var str = \x22The best things in life are free\x22;\r\n var patt = new RegExp(\x22e\x22);\r\n var res = patt.test(str);\r\n document.getElementById(\x22demo\x22).innerHTML = res;\r\n}\r\n"

     document.write(unescape(str));

Output:- function myFunction() { var str = "The best things in life are free"; var patt = new RegExp("e"); var res = patt.test(str); document.getElementById("demo").innerHTML = res; }

Kautuk Sahni
smacdonald2008
Level 10
May 11, 2018

That is an unusual requirement. Most of the  time when JS is used with HTL component - the JS is created as part of the HTL component - as discussed here: Adobe Experience Manager Help | Creating an Adobe Experience Manager HTML Template Language component that uses JavaScri…

(that is the 1st time I have heard of letting an author enter JS logic via a component dialog)

Level 2
May 11, 2018

Thankyou this helped.I have later used eval for evaluating and outputting the code updated there.

Level 2
May 11, 2018

smacdonald2008​ : Agree.Its not recommended for authors to have this option.