| Author | 
              Topic: Javascript Code for URL UTF-8 Encoder & Decorder  |  
           
         |  
        
          
            
              
                
                	
                  
                    
                      WebSpider member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            147 |  
                          
                            | joined: | 
                            06/29/2006 |  
                          
                            | from: | 
                            Seattle, WA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Javascript Code for URL UTF-8 Encoder & Decorder |  
                        
                          
<script language="javascript">
var Utf8Encoder = {
 
    // URL utf-8 encoding
    encode : function (str) {
        return escape(this._utf8_encode(str));
    },
 
    // URL utf-8 decoding
    decode : function (encoded_str) {
        return this._utf8_decode(unescape(encoded_str));
    },
 
    // UTF-8 encoding
    _utf8_encode : function (str) {
        str = str.replace(/\r\n/g,"\n");
        var encoded_str = "";
 
        for (var n = 0; n < str.length; n++) {
 
            var c = str.charCodeAt(n);
 
            if (c < 128) {
                encoded_str += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                encoded_str += String.fromCharCode((c >> 6) | 192);
                encoded_str += String.fromCharCode((c & 63) | 128);
            }
            else {
                encoded_str += String.fromCharCode((c >> 12) | 224);
                encoded_str += String.fromCharCode(((c >> 6) & 63) | 128);
                encoded_str += String.fromCharCode((c & 63) | 128);
            }
        }
        return encoded_str;
    },
 
    // UTF-8 decoding
    _utf8_decode : function (encoded_str) {
        var str = "";
        var i = 0;
        var c = c1 = c2 = 0;
 
        while ( i < encoded_str.length ) {
 
            c = encoded_str.charCodeAt(i);
 
            if (c < 128) {
                str += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = encoded_str.charCodeAt(i+1);
                str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = encoded_str.charCodeAt(i+1);
                c3 = encoded_str.charCodeAt(i+2);
                str += String.fromCharCode(((c & 15) << 12) |
                                   ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return str;
    }
}
</script>
 |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      WebSpider member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            147 |  
                          
                            | joined: | 
                            06/29/2006 |  
                          
                            | from: | 
                            Seattle, WA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | AJAX URL UTF-8 Encoding Example |  
                        
                          For website written in most western languages like English, any uer input value can be passed via AJAX GET method as the follows: 
 
    // get an XMLHttpRequest object
    XMLHttpRequest xmlhttp = new XMLHttpRequest();
    // URL to the target server
    var url = "myServer.jsp?uid=myValue";
    // send out the request
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(null);    
 If the value 'myValue' has special characters, a UTF-8 encryption is required before the value being sent out.
 
    // get an XMLHttpRequest object
    XMLHttpRequest xmlhttp = new XMLHttpRequest();
    // URL with UTF-8 encoded params
    var url_utf8 = "myServer.jsp?uid=" + Utf8Coder.encode(myValue);
    // send out the request
    xmlhttp.open("GET", url_utf8, true); 
    xmlhttp.send(null);    
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
      |