Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Sightly Javascript

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Administrator

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

View solution in original post

4 Replies

Avatar

Correct answer by
Administrator

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

Avatar

Level 10

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 componen...

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

Avatar

Level 2

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

Avatar

Level 2

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