简体   繁体   中英

Unterminated String literal - Javascript

I need to pass a parameter to Javascript function and that parameter may contain special characters like %. How do I escape these characters so that they are properly passed to JS? These parameters are generated dynamically so I cannot identify each and every single character and put an escape character there.

This is the JSP part...

<a href="javascript:redirect('<bean:write property="appId" name="appl"/>','<bean:write property="propId" name="appl"/>','<bean:write property="url" name="appl"/>');">
                                click this
                            </a>

this is the generated part...

<a href="javascript:redirect('A-0', '07', 'http://localhost:9083/xxxxx/aaa/bbbb/ccccc/redirectApply.do?token=9BsPtrGQPEYWv6dvbP9hsX9skeghTHnonkKZtsqrkZXSf7gKqKRYdo2i6CpcLOWxroqrWEDhyosk%0D%0A1aD1tQ0OBHHfsVjPKb%2B1kSFzjCg8lKyoGropGWc%2Bc7oCT8%2BYItwX');">

It looks like the character '%' is causing the issue. I looked at error console in Firefox and it shows the string upto ****WEDhyosk and says string is not terminated........

This question is related to an earlier question I posted , but since I didn't have a clear understanding of the issue back then, I am reposting,

Try something like this:

String encodedurl = URLEncoder.encode("www.google.com","UTF-8");

The browser should then automatically decode the string when the link is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM