Evilripper ‘ s Blog

A programmer’s work is never done

  • Home
  • Downloads
  • News
  • 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
Dicembre 2008
L M M G V S D
« Nov    
1234567
891011121314
15161718192021
22232425262728
293031  

Affiliates & Sponsor

Add to Technorati Favorites

BlogItalia.it - La directory italiana dei blog

Tophost Hosting

565 commenti spam
bloccato da
Akismet

Locations of visitors to this page

Blogroll

  • Rocco Blog
  • Ghetto Rimini
  • jumping head shot
  • vinoitaliano.net
  • Balla Con Noi
  • www.giourbinati.it
  • www.gyf.it
  • Lastchaos Italia
  • lu01 rock band
  • www.michelefocchi.com
  • CappellaiMatti
  • www.wizardtale.org

Categories

  • database (1)
  • development (10)
    • CSharp (3)
    • delphi (2)
    • javascript (1)
    • php (4)
    • vb.net (1)
  • hardware (3)
  • internet (19)
    • cms (1)
    • google (4)
    • wordpress (1)
  • linux (5)
    • gentoo (4)
  • software (3)
  • videogames (4)
    • unreal tournament 3 (1)
  • winXp (1)

Archives

  • 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

  • Call to undefined function: bcdiv()
  • Windows Xp si riavvia? E’ stato AVG :-D
  • Chi vuole un xbox a 99 euro?
  • Download Call Of Duty World At War Beta-Demo
  • C# Split con una String e non con un Char
  • Installare Samba On Gentoo
  • Installare Subversion Gentoo - How To
  • Catturare Enter Componente <Asp:Login>
  • Firefox 3 Download Day!!
  • Spegnere WinXp da riga di comando

Recent Comments

  • chinese new year firework in Tema Dark Per WinXP
  • 2005 new year in Tema Dark Per WinXP
  • christmas songs silent night in Tema Dark Per WinXP
  • break christmas girl party in Tema Dark Per WinXP
  • christmas co craft worker in Tema Dark Per WinXP
  • adding comment in picture in Tema Dark Per WinXP
  • marco di pietro in Virgin Radio Su WinAmp / Windows Media Player
  • Evilripper in Guida SingleClick - Teleassistenza Internet UltraV…
  • ghostfox in Guida SingleClick - Teleassistenza Internet UltraV…
  • Cheereecor in Apple iPhone Virtuale
  • Random Selection of Posts

    • Salvare filmati asf, wmv, wma, asx, wmx, wvx, wax
    • mySql DELETE ON CASCADE [myIsam]
    • Catturare Enter Componente <Asp:Login>
    • Raffreddamento a olio - PC Sottolio
    • Ole Office Automation Word Replace di una stringa [Delphi]
    • Ambienti di sviluppo per PHP
    • 8 Settembre Vaffanculo Day Beppe Grillo
    • Installare Modulo PHP6 Apache Win32
    • Virgin Radio Su WinAmp / Windows Media Player
    • Download Call Of Duty World At War Beta-Demo
© 2008 Evilripper ‘ s Blog is proudly powered by WordPress
Theme designed by Roam2Rome