SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    escape() behaves strange sometimes

    Hi,

    Yesterday I discovered a problem on one of my sites: escape function was encoding the same string Salò in two different ways:
    1. Sal%F2
    2. Sal%C3%B2


    This was happening on Firefox 2.0.0.3. In Opera 9.20 was encoded ok every time.

    So, in Firefox was a russian roulette . Sometimes was encoding the string like I wanted to, sometimes not. As a solution I use encodeURIComponent(), but the behavior of escape() seems strange to me.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    escape and unescape are deprecated, use encodeURI instead

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    escape and unescape are deprecated, use encodeURI instead
    Ok, this I understand, as I already read some topics like escape() vs. encodeURI() vs. encodeURIComponent() and I use encodeURIComponent() to get the work done.

    I am just wondering why escape() has this behavior on Firefox!

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just get the first result with Firefox 2.0.0.3

  5. #5
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It happened for me to get both. And I am 100% sure I was using only escape().

  6. #6
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since both encodings return the same value when decoded, Salò,
    what does it matter that escape encodes it Sal%F2
    and encodeURIComponent uses Sal%C3%B2?

    The thing to remember is how you encoded it, and use the matching decoder method..

    You would get a malformed sequence error if you pass the escaped value to decodeURIComponent, and unescaping the URI code returns Salò.
    Last edited by mrhoo; May 12, 2007 at 15:13.

  7. #7
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The difference was on the server side: I send that parameter to a php and use it in an SQL query, but not before utf8_decode() that value. And when is sent Sal%C3%B2 it works like I expect to. Maybe I did wrong by using utf8_decode() on a javascript escape() value but escape() behavior is strange.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Don't use escape() with UTF-8 as it doesn't support it - that's why it is deprecated.

    encodeURI() and encodeURIComponent() are the replacements which also support non-ASCII characters.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •