8

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"));
holden
  • 13,471
  • 22
  • 98
  • 160
  • As you tagged this question with *encodeuricomponent*, haven’t you tried that? – Gumbo Sep 07 '10 at 15:28
  • 1
    encodeURIComponent should work. I've tried the sample you've provided and it seems working. Look at http://jsfiddle.net/XdJCM/. – Zafer Sep 07 '10 at 15:39

2 Answers2

17

Try with encodeURIComponent instead.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
6

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.

KatieK
  • 13,586
  • 17
  • 76
  • 90
Stevie B
  • 61
  • 1
  • 1