Urlencode and Urldecode with JavaScript
User Rating: / 1
PoorBest 
Written by Girish Singh   







javascript_img_for_intro.pngUrlencoding is done to replace non URL safe characters with URL safe characters in a string. This is particularly done when either passing GET parameters through the browser URL or when passing data through AJAX. Javascript provides various functions which implement this functionality

escape(string) - This function is used to encode strings. The escape() function encodes special characters, with the exception of * @ - _ + . /
unescape(string) should be used to decode the string encoded by the escape function.
For example
var str = "hello34ad#4";
alert(escape(str));
alert(unescape(str));

Please note that it is not recommended to encode URI by escape instead we should use the functions listed below.

 

encodeURI(string) - The encodeURI() function encodes a string as a URI. The encodeURI() function encodes special characters, with the exception of:
, / ? : @ & = + $ #. Use the decodeURI() function to decode URIs encoded with encodeURI().
For example
var str = 'http://www.yahoo.com';
alert(encodeURI(str));
alert(decodeURI(str));



encodeURIComponent(string) - The encodeURI() function encodes a string as a component of a URI. This function does not have any exception. Use the decodeURIComponent(string) function to decode the URI encoded by this function.

 

Problem with Javascript Encoding function

Although JavaScript provided many url encoding and decoding function. There is an inherent problem in all of them which is that they are not compatible with the PHP urlencode and urldecode functions.
This problem is particularly an issue when using ajax to communicate between JavaScript and PHP. The standard JavaScript functions operate slightly differently: they encode space as "%20", and treat "+" as a safe character. However we can write a JavaScript function which can solve this problem. The two functions given below can encode and decode the URL in a fashion similar to the PHP counterparts.

function URLEncode(url) //Function to encode URL.
{
// The Javascript escape and unescape functions do not correspond
// with what browsers actually do...
var SAFECHARS = "0123456789" + // Numeric
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()"; // RFC2396 Mark characters
var HEX = "0123456789ABCDEF";

var plaintext = url;
var encoded = "";
for (var i = 0; i < plaintext.length; i++ ) {
var ch = plaintext.charAt(i);
if (ch == " ") {
encoded += "+"; // x-www-urlencoded, rather than %20
} else if (SAFECHARS.indexOf(ch) != -1) {
encoded += ch;
} else {
var charCode = ch.charCodeAt(0);
if (charCode > 255) {
alert( "Unicode Character '"
+ ch
+ "' cannot be encoded using standard URL encoding.\n" +
"(URL encoding only supports 8-bit characters.)\n" +
"A space (+) will be substituted." );
encoded += "+";
} else {
encoded += "%";
encoded += HEX.charAt((charCode >> 4) & 0xF);
encoded += HEX.charAt(charCode & 0xF);
}
}
}

return encoded;
};


 

function URLDecode(url) //function decode URL
{
// Replace + with ' '
// Replace %xx with equivalent character
// Put [ERROR] in output if %xx is invalid.
var HEXCHARS = "0123456789ABCDEFabcdef";
var encoded = url;
var plaintext = "";
var i = 0;
while (i < encoded.length) {
var ch = encoded.charAt(i);
if (ch == "+") {
plaintext += " ";
i++;
} else if (ch == "%") {
if (i < (encoded.length-2)
&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
plaintext += unescape( encoded.substr(i,3) );
i += 3;
} else {
alert( 'Bad escape combination near ...' + encoded.substr(i) );
plaintext += "%[ERROR]";
i++;
}
} else {
plaintext += ch;
i++;
}
} // while

return plaintext;
};

 

 







 

Quote this article on your site

  Be first to comment this article

Only registered users can write comments.
Please login or register.


Powered by AkoComment Tweaked Special Edition v.1.4.6
AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com. All right reserved

 
< Prev   Next >




Privacy Notice | Advertising Info | Feedback | Contact Us | Partners

The information and views presented above are by the author. Mabaloo.com does not take any gurantee of accuracy.
The views expressed above are that of the author and in no way related to mabaloo.com
Highlite It
© 2007 @ mabaloo.com.