javascript and bad character ?

Last post 05-14-2008 3:21 AM by christopheEU. 6 replies.

Sort Posts:

  • javascript and bad character ?

    05-13-2008, 4:54 AM

    Hi,

    I'm a beginner with javascript and asp net, i work with visual studio 2005.

    I have a javascript function who add a value in a listbox:

    function ListAddItem(listBoxId, value)

    {

    ...

    }

    This function is on a parent page and on this page i have several links.

    when i click on a link a child page opens, on this page there are some text box, i enter some string  and when i click the button

    i call the opener.ListAddItem

    script += "opener.ListAddItem('list1", '" + Value + "');";

    that work of course, but there is a problem if i enter some string who contains some '  !!!

    Ok before to call the function javascript i use string.replace("'","''"); in c sharp !

    but that doesn't work because when i execute the code and enter a string with a ', the ' is well double '' but now my function fails because he think there are 3 parameter and not 2!!!

    Have you got any idea ? 

    Thanks in advance

    Christophe

  • Re: javascript and bad character ?

    05-13-2008, 5:09 AM
    • Participant
      1,250 point Participant
    • jagan.unique
    • Member since 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237

     

    use escape characters code while sending ' (apostrophe) in a parameter. other wise it will treat as another parameter value and doesn't executes your code..

     
    Ex: ' (for apostrophe)

    list of escape characters..

    http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php 

    Click “Mark as Answer” on the post(s) that helped you.
  • Re: javascript and bad character ?

    05-13-2008, 5:34 AM

    Hi,

    Thanks for your fast reply !

    now if i put your code in my code c sharp like :

     Value = Value.Replace("'", "'");

    i execute the application, no fail when i enter value with ' but when the page is end of loading i see my value as for example:

    n'avez vous => i see after loading => n'avez vous !!!

    And of course, i'd like to see n'avez vous

    Any idea ?

    Thanks

    Christophe

  • Re: javascript and bad character ?

    05-13-2008, 5:50 AM
    • Participant
      1,250 point Participant
    • jagan.unique
    • Member since 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237

    christopheEU:
    n'avez vous => i see after loading => n'avez vous !!!

    And of course, i'd like to see n'avez vous

    Any idea ?

     

    again replace ' with ' 

     some think lik this.. Value = Value.Replace("'","'");

    Click “Mark as Answer” on the post(s) that helped you.
  • Re: javascript and bad character ?

    05-13-2008, 7:15 AM

    H JaganUnique,

    if i understand, you tell me place code :

    c sharp :Value = Value.Replace("'","'");

    and on javascript for to delete ' and replace by ' put:

     Value = Value.Replace("'","';");

    is it right ?

    But, if just to test something and it's works

    In my code c sharp, now i replace the ' by \\' 
    Value = Value.Replace("'", "\\'");

     And it's works fine, when i click on the button no javascript fails and when the page is loading i see my string as : n'avez vous !

    Now, why the double \\ ?

    thanks 

    Christophe

  • Re: javascript and bad character ?

    05-14-2008, 12:47 AM
    Answer
    • Participant
      1,250 point Participant
    • jagan.unique
    • Member since 02-08-2008, 9:42 AM
    • Hyderabad, India
    • Posts 237

    Hi christopheEU ,

    I found a better way for sending apostrophe as a parameter value.. use \ followed by ' will solve your problem..
     

    Ex.

     <script type="text/javascript">
    function ShowVaue(ctrl,v)
    {
    alert(v);
    //document.getElementById("Label1").value=v;

    }
        </script>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowVaue('d','n\'avez vous');" /> 

     

     

    Click “Mark as Answer” on the post(s) that helped you.
  • Re: javascript and bad character ?

    05-14-2008, 3:21 AM

    Hi Jagan.unique,

    thanks for your reply ...that nice that work !

    but, for to resolve my problem in my code c sharp now, i replace the ' by \\' and i have made some test and that's works !

    your solution work too, but i don't know if the user has enter a ' or not !

    As you can see i'm a real beginner :-) 

    Thanks for your time

    Christophe

Page 1 of 1 (7 items)