Evilripper Blog

A programmer's work is never done

  • Home
  • Downloads
  • News
  • Sfondi Desktop
  • About Me

30

ott

Urlencode Javascript – Urldecode Php

Posted by evilripper  Published in development, javascript

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:

  1.  
  2. // ====================================================================
  3.  
  4. //       URLEncode and URLDecode functions
  5.  
  6. //
  7.  
  8. // Copyright Albion Research Ltd. 2002
  9.  
  10. // http://www.albionresearch.com/
  11.  
  12. //
  13.  
  14. // You may copy these functions providing that
  15.  
  16. // (a) you leave this copyright notice intact, and
  17.  
  18. // (b) if you use these functions on a publicly accessible
  19.  
  20. //     web site you include a credit somewhere on the web site
  21.  
  22. //     with a link back to http://www.albionresearch.com/
  23.  
  24. //
  25.  
  26. // If you find or fix any bugs, please let us know at albionresearch.com
  27.  
  28. //
  29.  
  30. // SpecialThanks to Neelesh Thakur for being the first to
  31.  
  32. // report a bug in URLDecode() – now fixed 2003-02-19.
  33.  
  34. // And thanks to everyone else who has provided comments and suggestions.
  35.  
  36. // ====================================================================
  37.  
  38. function URLEncode(str)
  39.  
  40. {
  41.  
  42.  // The Javascript escape and unescape functions do not correspond
  43.  
  44.  // with what browsers actually do…
  45.  
  46.  var SAFECHARS = "0123456789" +                                 // Numeric
  47.  
  48.                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
  49.  
  50.                                 "abcdefghijklmnopqrstuvwxyz" +
  51.  
  52.                                 "-_.!~*’()";                                    // RFC2396 Mark characters
  53.  
  54.  var HEX = "0123456789ABCDEF";  var plaintext = str;
  55.  
  56.  var encoded = "";
  57.  
  58.  for (var i = 0; i < plaintext.length; i++ ) {
  59.  
  60.         var ch = plaintext.charAt(i);
  61.  
  62.      if (ch == " ") {
  63.  
  64.             encoded += "+";                             // x-www-urlencoded, rather than %20
  65.  
  66.         } else if (SAFECHARS.indexOf(ch) != -1) {
  67.  
  68.             encoded += ch;
  69.  
  70.         } else {
  71.  
  72.             var charCode = ch.charCodeAt(0);
  73.  
  74.                 if (charCode > 255) {
  75.  
  76.                     alert( "Unicode Character ‘"
  77.  
  78.                         + ch
  79.  
  80.                         + "’ cannot be encoded using standard URL encoding.\n" +
  81.  
  82.                                   "(URL encoding only supports 8-bit characters.)\n" +
  83.  
  84.                                           "A space (+) will be substituted." );
  85.  
  86.                         encoded += "+";
  87.  
  88.                 } else {
  89.  
  90.                         encoded += "%";
  91.  
  92.                         encoded += HEX.charAt((charCode >> 4) &amp; 0xF);
  93.  
  94.                         encoded += HEX.charAt(charCode &amp; 0xF);
  95.  
  96.                 }
  97.  
  98.         }
  99.  
  100.  } // for
  101.  
  102. //document.URLForm.F2.value = encoded;
  103.  
  104.  return encoded;
  105.  
  106.  //return false;
  107.  
  108. };

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.

no comment

Multilanguages

  • English
  • Italiano

My Flikr

Flickr in the skyInto the whiteiced treesthe thin white lineiced treeSit a momentA lampiced treecollectionHot day :-D

Social Connection







Affiliates & Sponsor

Add to Technorati Favorites

Il Bloggatore


BlogItalia.it - La directory italiana dei blog

Tophost Hosting

7.533 commenti spam
bloccato da
Akismet

Locations of visitors to this page

Blogroll

  • Rocco Blog
  • jumping head shot
  • The Developers
  • gyf.it
  • lu01 rock band
  • Michele Focchi

Categories

  • database (1)
  • development (13)
    • CSharp (4)
    • delphi (2)
    • javascript (1)
    • php (5)
    • vb.net (1)
  • hacking (2)
  • hardware (12)
    • mobile (1)
  • internet (40)
    • cms (1)
    • google (9)
    • wordpress (1)
  • linux (5)
    • gentoo (4)
  • microsoft (2)
  • Senza categoria (3)
  • software (8)
  • videogames (6)
    • unreal tournament 3 (1)
  • winXp (2)

Archives

  • marzo 2010 (2)
  • febbraio 2010 (6)
  • gennaio 2010 (6)
  • dicembre 2009 (4)
  • novembre 2009 (1)
  • ottobre 2009 (5)
  • settembre 2009 (3)
  • luglio 2009 (2)
  • maggio 2009 (1)
  • aprile 2009 (1)
  • marzo 2009 (2)
  • febbraio 2009 (1)
  • gennaio 2009 (2)
  • dicembre 2008 (3)
  • novembre 2008 (4)
  • settembre 2008 (3)
  • giugno 2008 (3)
  • maggio 2008 (1)
  • aprile 2008 (1)
  • marzo 2008 (1)
  • febbraio 2008 (4)
  • gennaio 2008 (4)
  • dicembre 2007 (2)
  • novembre 2007 (4)
  • ottobre 2007 (7)
  • settembre 2007 (7)
  • agosto 2007 (2)
  • luglio 2007 (1)
  • giugno 2007 (2)

Recent Entries

  • Gli Hackers non sono crackers
  • Recuperare le password da Filezilla
  • Google colpevole libertà di espressione in pericolo!
  • Errore di Chrome 0xc0000005
  • Windows7 Rileverà le Copie Pirata con il WTA
  • In Italia Filtrato Ip di The Pirate Bay!
  • Condividere con Google Buzz
  • I Migliori Giochi Gratis Online
  • Spreaker una nuova radio 2.0
  • Cellulare come router Wi-Fi

Recent Comments

  • Andrea Carnera in AmigaOne X1000 Amiga came back?<…
  • GREY_FOX in Hackers are not crackersGoogle guilty for violation of privacyaddalo.it in Google guilty for violation of privacy
  • diggita.it in Google guilty for violation of privacy
  • GREY_FOX in Google guilty for violation of privacy
  • Elena in Spreaker a new radio 2.0AmigaOne X1000 Amiga came back?<…">luca in AmigaOne X1000 Amiga came back?<…
  • Daniele in AmigaOne X1000 Amiga came back?<…
  • Daniele in Share With Google Buzz…
  • addalo.it in The Pirate Bay Ip Filtered In Italy!