0

Iam trying to encode url of the xmlhttprequest method as utf-8

var Url=("http://localhost/day1/tryconnect.php?add="+address)  ;       // the server script to handle the request
if (window.XMLHttpRequest) {
       xmlhttp= new XMLHttpRequest() ;        // For all modern browsers
       } 

 else if (window.ActiveXObject) {
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") ;   // For (older) IE
 }
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("connection is stable") ;
}


 xmlhttp.open("GET", Url, false);   
 xmlhttp.send(null);
 var xml = xmlhttp.responseXML ; 

i tried the following method to encode the Url http://www.webtoolkit.info/javascript-utf8.html
it didn't work for me

John silver
  • 105
  • 1
  • 6
  • 14
  • If you want to encode a string use encodeURIComponent('your'+ string) or escape('your'+ string). Hope it helps you. – Jan Wiemers Mar 21 '12 at 10:54
  • it didnt help in my case...the problem that in the url http://localhost/day1/tryconnect.php?add="+address (+address) is a special character word..and its being sent to php code via ajax request using the xmlhttprequest object...the special all am looking for is to probably encode this address in utf-8 so that it won't make any problems dealing with Chrome and IE..apparently the address is received corrupted and not encoded properly... – John silver Mar 21 '12 at 11:07
  • solved :) http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – John silver Mar 21 '12 at 11:29

1 Answers1

2

Here's a great article on the subject of URLEncoding/Decoding in JavaScript: http://roneiv.wordpress.com/2007/12/25/how-to-do-proper-url-encoding-in-javascript-when-using-windowopen/

It uses a combination of encodeURI + escape

encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83