Escaping ' in Javascript ?

Discussion in 'ASP .Net' started by Ben, Nov 7, 2003.

  1. Ben

    Ben Guest

    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
     
    Ben, Nov 7, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:076a01c3a501$e3be3670$...
    > 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
     
    Munsifali Rashid, Nov 7, 2003
    #2
    1. Advertising

  3. Ben

    Ben Guest

    Many thanks to you :)


    >-----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" <> wrote in

    message
    >news:076a01c3a501$e3be3670$...
    >> 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

    >
    >
    >.
    >
     
    Ben, Nov 7, 2003
    #3
  4. Ben

    mikeb Guest

    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=

    for a routine that will escape most everything properly.

    --
    mikeb
     
    mikeb, Nov 10, 2003
    #4
  5. Ben

    mikeb Guest

    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=
    >
    >
    > 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
     
    mikeb, Nov 10, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Durzi

    Escaping a CrLf in JavaScript

    George Durzi, Jul 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,419
    George Durzi
    Jul 6, 2004
  2. Ghosty

    Javascript escaping problem

    Ghosty, Jun 14, 2007, in forum: Java
    Replies:
    0
    Views:
    433
    Ghosty
    Jun 14, 2007
  3. Lucian Sandor

    Javascript escaping madness

    Lucian Sandor, Jul 30, 2004, in forum: Javascript
    Replies:
    5
    Views:
    184
    Randy Webb
    Aug 1, 2004
  4. Replies:
    2
    Views:
    195
  5. Gene Wirchenko

    JavaScript's Character Escaping Functions

    Gene Wirchenko, Dec 30, 2011, in forum: Javascript
    Replies:
    9
    Views:
    677
    Dr J R Stockton
    Jan 2, 2012
Loading...

Share This Page