Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to encode to avoid javascript error

 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a piece of javascript code to submit a form with a hidden variable - "err". Before submitting the form, I assign the value of description to "err", the value of description is generated by some other code.



But because the value of description may contains any text including special characters like quote mark, slash, e.g. "xx", "</xx>", it generate javascript error.

The javascript error l encounter is:

1) When description with value = Attribute "identifier" is in the tag
It shows javascript error: "missing ; before statement"
document.forms["debug"].err.value = "Attribute "identifier" is in the tag";

2)When description = Expect "</identifier>" to terminate
It shows javascript error: unterminated regular expresson literal
document.forms["debug"].err.value = "Expect "</identifier>" to terminate";

How to handle this situation? Is there something like encoding to avoid this problem?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try the escape() method.

Eric
 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric, thanks for your reply.

But it does not work if I use escape() javascript function, same error actually. I believe the escape() is used to convert those special characters for URL, like &, =, etc..

In my case, the problem is how to avoid the situation like below in javascript statement, the quote mark inside and the slash are causing javascript syntax error.

"avoid "quote mark" in a text"
"avoid "</something like this>" in javascript"

Any help is greatly appreciated!!
 
Michael L. Zhang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the value of "description" is from java code, I guess I need do some conversion over there instead of doing it on client side. Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael L. Zhang:
Since the value of "description" is from java code, I guess I need do some conversion over there instead of doing it on client side. Thanks.


Correct. The escape() function will URL-encode the string. Not what you want.

There is no built-in Javascript string literal encoder on either the client or server side. So your best bet is to build one for your server-side code and pass the string through it prior to sending it to the view, or perhaps create a JSP custom action you can use on the JSP to perform the encoding. I would not try to do this on the client.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


So your best bet is to build one for your server-side code and pass the string through it prior to sending it to the view, or perhaps create a JSP custom action you can use on the JSP to perform the encoding.


Are there any open source custom tag for this?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic