Forum Moderators: open

Message Too Old, No Replies

escaping javascript characters

escaping reserved characters

         

kadnan

5:59 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



i am making a function in which text is being passed as a function parameter. The problem is that when a user passes a ( or ] then script throw unterminated string constant. How can I escape these things?

thanks

Fotiman

6:19 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you give an example of what you mean? Something like this should work:

var textvalue = "(";
function doSomething(t) {
alert(t);
}
doSomething(textvalue);

kadnan

6:51 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



ok. I will like to add that i am getting "unterminated string constant" error though I am using addslahes() method of php for escape sequence purpose.

what I think that it's being due to new line character with in html. Forinstance

<b>
<font>
do
</font>
</b>

rocknbil

4:01 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I'm getting you right, you have to insure the escape is output in your html. Using Fotiman's example,

function doSomething(t) {
alert(t);
}
doSomething("this \(is my\) textvalue");

From PHP or perl, the server side language will interpret the escape so on output you still get

function doSomething(t) {
alert(t);
}
doSomething("this (is my) textvalue");

Which will error. So what you want is to "escape the escape" from your server side language,

function doSomething(t) {
alert(t);
}
doSomething("this \\(is my\\) textvalue");

so the \ remains in your output.

Same is true of newlines you want to be interpreted by Javascript. \\n, not \n.