Escaping ' in Javascript ?

B

Ben

I tried to pass in a query string to a open window via
the following codes :

string strName = tbName.Text.Trim();

Response.Write("<SCRIPT> javascript:window.open
('Search.aspx?Name=" + strName
+ "', 'Search', 'width=600,height=550,left=280,top=100,scr
ollbars=1,resizable=1,status=1') </SCRIPT>");

It works fine if the var value is for eg. John. However
when the var value contains the ' char, it hits into
error.

How do you escape the ' char in this case?

Thanks,
Ben
 
M

Munsifali Rashid

You can escape the apostophe by using double backlashes (in C#). One
backslash is to escape the apostrophe in C#, and the second is to escape it
in JavaScript.

If you're using VB.NET, you only need one backslash.

C#: strName = strName.Replace("'", "\\'");
VB.NET: strName = strName.Replace("'", "\'")

Hope this helps,

Mun
 
B

Ben

-----Original Message-----
You can escape the apostophe by using double backlashes (in C#). One
backslash is to escape the apostrophe in C#, and the second is to escape it
in JavaScript.

If you're using VB.NET, you only need one backslash.

C#: strName = strName.Replace("'", "\\'");
VB.NET: strName = strName.Replace("'", "\'")

Hope this helps,

Mun







.
 
M

mikeb

Ben said:
I tried to pass in a query string to a open window via
the following codes :

string strName = tbName.Text.Trim();

Response.Write("<SCRIPT> javascript:window.open
('Search.aspx?Name=" + strName
+ "', 'Search', 'width=600,height=550,left=280,top=100,scr
ollbars=1,resizable=1,status=1') </SCRIPT>");

It works fine if the var value is for eg. John. However
when the var value contains the ' char, it hits into
error.

How do you escape the ' char in this case?

See this posting:


http://groups.google.com/[email protected]

for a routine that will escape most everything properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top