*
The moose likes HTML, CSS and JavaScript and the fly likes how to encode to avoid javascript error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to encode to avoid javascript error" Watch "how to encode to avoid javascript error" New topic
Author

how to encode to avoid javascript error

Michael L. Zhang
Ranch Hand

Joined: Jul 06, 2003
Posts: 33
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
try the escape() method.

Eric
Michael L. Zhang
Ranch Hand

Joined: Jul 06, 2003
Posts: 33
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

Joined: Jul 06, 2003
Posts: 33
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
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?


My blood is tested +ve for Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to encode to avoid javascript error