Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to Escape "&" on xmlHttp

    Hello,
    I have an Ajax submit xmlHttp. It works fine except some special characters like the ampersand do not pass and cut the submitted text. I understood that I could use the "escape" function to solve this. I have no idea how to use escape on my Java code.

    Here is my code, how can I add the "escape function"?

    Code:
    <script type="text/javascript">
    var fieldname = 'postrowbody';
    var xmlHttp = http_object();
    
    function get(f)
    {
       var param = elem = '';
       for(var s = 0; s < f.elements.length; s++)
       { 
          elem = f.elements[s]; 
          if(param != '')
          { 
             param += '&'; 
          } 
          param += elem.name + '=' + elem.value; 
       }
       document.getElementById(fieldname).innerHTML += "<div id='load' class='preloader'><img src='/tabs/loading.gif' /><p>Requesting content...</p></div></div>";
    
       xmlHttp = http_object();
       xmlHttp.onreadystatechange = handle_return;
       xmlHttp.open("POST", '{S_AJAX_ACTION}', true);
       xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       xmlHttp.send(param);
       
       //clear the message site
       document.postform.message.value = '';
    }
    
    function handle_return()
    {
       if (xmlHttp.readyState == 4)
       {
          var load = document.getElementById('load');
          load.parentNode.removeChild(load);
          document.getElementById(fieldname).innerHTML += xmlHttp.responseText;
          document.postform.reset();
       }
    }
    
    function http_object()
    {
       try
       {
          // Firefox, Opera 8.0+, Safari
          xmlHttp = new XMLHttpRequest();
       }
       catch (e)
       {
          // Internet Explorer
          try
          {
             xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
          }
          catch (e)
          {
             try
             {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
             }
             catch (e)
             {
                alert("Your browser does not support AJAX!");
                return false;
             }
          }
       }
       return xmlHttp;
    }
    </script>
    Thank you

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this way:
    Code:
    <script type="text/javascript">
    var fieldname = 'postrowbody';
    var xmlHttp = http_object();
    
    function get(f)
    {
       var param = elem = '';
       for(var s = 0; s < f.elements.length; s++)
       { 
          elem = f.elements[s]; 
          if(param != '')
          { 
             param += '&amp;'; 
          } 
          param += elem.name + '=' + escape(elem.value);
    best regards

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Example:-

    var age = 37;
    var name = 'John Doe';
    var job = 'Dog Catcher';
    var passData = 'age='+escape(age)+'&name='+escape(name)+'&job='+escape(job);


    The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #4
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx oesxyl & Philip M for the fast response. It did the trick!

    oesxyl, can you also please tell me how to do if I want to another character besides "&" - things like ()#%&

    Thanx again for your help!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    See above Post#3.

    escape('~!@#$%^&*(){}[]=:/,;?+\'"\\');
    returns
    %7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by MirceaP View Post
    Thanx oesxyl & Philip M for the fast response. It did the trick!

    oesxyl, can you also please tell me how to do if I want to another character besides "&" - things like ()#%&

    Thanx again for your help!
    you can use instead of chars their entities values:

    http://www.evotech.net/blog/2007/04/...numeric-order/

    but in xml only "&", "'", '"', '<', '>' are not permited so you can use '&amp;', '&apos;', '&quot;', '&lt;' and '&gt;' for them and utf-8 for the rest.

    javascript escape will convert to %nn any char is not allwed and, as Philip M explain, assuming that paramaters names are valid you need to escape only their values.

    best regards

  • #7
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx for your help, I really appreciate it!
    All the best,
    Mircea


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •