Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Escaping ' in Javascript ?

Reply
Thread Tools

Escaping ' in Javascript ?

 
 
Ben
Guest
Posts: n/a
 
      11-07-2003
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
 
Reply With Quote
 
 
 
 
Munsifali Rashid
Guest
Posts: n/a
 
      11-07-2003
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




"Ben" <(E-Mail Removed)> wrote in message
news:076a01c3a501$e3be3670$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Ben
Guest
Posts: n/a
 
      11-07-2003

>-----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
>
>
>
>
>"Ben" <(E-Mail Removed)> wrote in

message
>news:076a01c3a501$e3be3670$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
mikeb
Guest
Posts: n/a
 
      11-10-2003
Ben wrote:

> 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/groups?selm...TNGP10.phx.gbl

for a routine that will escape most everything properly.

--
mikeb

 
Reply With Quote
 
mikeb
Guest
Posts: n/a
 
      11-10-2003
mikeb wrote:

> Ben wrote:
>
>> 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/groups?selm...TNGP10.phx.gbl
>
>
> for a routine that will escape most everything properly.
>
> --
> mikeb
>


I should also say that since you're passing the string in a URL query
string parameter, you should also run it through:

HttpUtility.UrlEncode()

to make sure the string is safe for URLs.

--
mikeb

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript's Character Escaping Functions Gene Wirchenko Javascript 9 01-02-2012 07:10 PM
Javascript escaping problem Ghosty Java 0 06-14-2007 02:52 PM
Escaping/encoding in an href with javascript j.e.frank@gmail.com Javascript 2 09-05-2006 01:28 PM
Javascript escaping madness Lucian Sandor Javascript 5 08-01-2004 10:48 PM
Escaping a CrLf in JavaScript George Durzi ASP .Net 0 07-06-2004 05:03 AM



Advertisments