Evilripper Blog

A programmer's work is never done

  • Home
  • Downloads
  • News
  • Wallpapers
  • About Me

30

Oct

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,544 spam comments
blocked by
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

  • March 2010 (2)
  • February 2010 (6)
  • January 2010 (6)
  • December 2009 (4)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (3)
  • July 2009 (2)
  • May 2009 (1)
  • April 2009 (1)
  • March 2009 (2)
  • February 2009 (1)
  • January 2009 (2)
  • December 2008 (3)
  • November 2008 (4)
  • September 2008 (3)
  • June 2008 (3)
  • May 2008 (1)
  • April 2008 (1)
  • March 2008 (1)
  • February 2008 (4)
  • January 2008 (4)
  • December 2007 (2)
  • November 2007 (4)
  • October 2007 (7)
  • September 2007 (7)
  • August 2007 (2)
  • July 2007 (1)
  • June 2007 (2)

Recent Entries

  • Hackers are not crackers
  • FileZilla Password Recovery
  • Google guilty for violation of privacy
  • Google Chrome bug Error 0xc0000005
  • Windows7 Targets Cracked Activations
  • The Pirate Bay Ip Filtered In Italy!
  • Share With Google Buzz
  • Best Free Online Games
  • Spreaker a new radio 2.0
  • Mobile as 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!