Urlencode Javascript – Urldecode Php

Siccome dovevo passare un testo tramite GET con ajax mi ero ritrovato con un piccolo problema di caratteri! :-( per esempio se c’era un & commerciale mi si troncava etc…

Visto che escape() di javascript non e’ full compatibile con la funzione di decodifica di php, ho trovato questo script per fare un urlencode in javascript cosi’ riesco a decodificarlo lato server tramite la funzione urldecode() di php:

// ====================================================================
 
//       URLEncode and URLDecode functions
 
//
 
// Copyright Albion Research Ltd. 2002
 
// http://www.albionresearch.com/
 
//
 
// You may copy these functions providing that
 
// (a) you leave this copyright notice intact, and
 
// (b) if you use these functions on a publicly accessible
 
//     web site you include a credit somewhere on the web site
 
//     with a link back to http://www.albionresearch.com/
 
//
 
// If you find or fix any bugs, please let us know at albionresearch.com
 
//
 
// SpecialThanks to Neelesh Thakur for being the first to
 
// report a bug in URLDecode() - now fixed 2003-02-19.
 
// And thanks to everyone else who has provided comments and suggestions.
 
// ====================================================================
 
function URLEncode(str)
 
{
 
 // 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 = str;
 
 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);
 
 		}
 
 	}
 
 } // for
 
//document.URLForm.F2.value = encoded;
 
 return encoded;
 
 //return false;
 
};

Link & Fonti

Il codice l’ho preso da http://www.albionresearch.com/misc/urlencode.php ed e’ stato adattato in una funzione a cui si passa il valore da codificare e restituisce il testo codificato.

Pubblicità Google Adsense

Se ti è piaciuta la notizia condividila con twitter o facebook...

Nel mio template dei commenti ho rimosso il tag nofollow se vuoi puoi farlo anche tu con il plugin nofollowfree

Segui il feed rss relativo a questa notizia: sottoscrivi feed commenti


Trackback & Pingback

Se vuoi rispondere a questa notizia o usarla sul tuo spazio internet usa l'url di trackback e il tuo sito apparirà nella lista dei trackback e pingback:


    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

    Ricevi un avviso se ci sono nuovi commenti. Oppure iscriviti senza commentare.