SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    EndodeURI problem

    Hello. I have a file named as Porto;.png . It contains semicolon. To append to an URL, I try to encodeURI its name . But encodeURI("Porto;.png") doesnt help. I expect encodeURI to report back as Porto%3b.png . What is the problem here?

    Thanks in advance.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use encodeURIComponent

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by owerio View Post
    Hello. I have a file named as Porto;.png . It contains semicolon. To append to an URL, I try to encodeURI its name . But encodeURI("Porto;.png") doesnt help. I expect encodeURI to report back as Porto%3b.png . What is the problem here?

    Thanks in advance.
    Try encodeURIComponent

    You're welcome in advance.
    Tab-indentation is a crime against humanity.

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Use encodeURIComponent
    And encode only the values, not the variable names. If your variable names are such that they need to be encoded, you should change them because that will screw you in other ways.

  5. #5
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    And encode only the values, not the variable names. If your variable names are such that they need to be encoded, you should change them because that will screw you in other ways.
    I havent understand this actually. Could you please give an example?

    By the way, encodeURIComponent encodes & . This isnt desired fo my case. But I also need to encode ; which encodeURI cant. I am totally confused which to use. I dont know too much about url encoding. I just want to combine the file name in encoded form with a valid url. How can I achieve this?

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by owerio View Post
    I havent understand this actually. Could you please give an example?
    If you have this:

    Code:
    var queryStr = "?foo1=" + bar1 + "&foo2=" + bar2;
    encode it like this:

    Code:
    var queryStr = "?foo1=" + encodeURIComponent(bar1) + "&foo2=" + encodeURIComponent(bar2);
    As the function name suggests, you are encoding only a certain component of the URI rather than the whole query string.

    Quote Originally Posted by owerio View Post
    I just want to combine the file name in encoded form with a valid url. How can I achieve this?
    Following from the above example, try this:

    Code:
    var queryStr = "?filename=" + encodeURIComponent(filename);

  7. #7
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I got it.


Tags for this Thread

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
  •