jQuery encodeURI for href not working

14,106

Solution 1

Try with encodeURIComponent instead.

Solution 2

I thought I was having the same problem, but then I realized I was looking at the results in both the mouseOver of the resulting link and in the address bar after clicking the link. It turns out that my browser was showing the spaces in both cases - i.e. when I copied the URI out of the address bar and pasted it in emacs the %20 revealed itself.

Share:
14,106

Related videos on Youtube

holden
Author by

holden

Updated on June 04, 2022

Comments

  • holden
    holden almost 2 years

    I'm having problems encoding a string so I can place a variable into a link. I'm sure this is really simple, but i had trouble turning anything up.

    $("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI("text o"));
    

    This doesn't work.

    encodeURI("text o")
    

    Still returns:

    link/text o
    

    Instead of:

    link/text%20
    

    Also tried:

    $("a.inquiry").attr("href", encodeURIComponent("/inquiry/6933/text=" + "text o"));
    
    • Gumbo
      Gumbo over 13 years
      As you tagged this question with encodeuricomponent, haven’t you tried that?
    • Zafer
      Zafer over 13 years
      encodeURIComponent should work. I've tried the sample you've provided and it seems working. Look at jsfiddle.net/XdJCM.
  • holden
    holden over 13 years
    that does work either ;-( either its not working or something else I'm doing is undoing the encoding afterwards?
  • Sarfraz
    Sarfraz over 13 years
    @holden: It could be so, encodeURIComponent has worked for me always.
  • Govind Singh
    Govind Singh about 10 years
    not working with me also, actually working with comma but not for spaces
  • user3761308
    user3761308 almost 7 years
    Wow, this was the solution for me too.