0

The 'src' property of an image is encoded when assigned. Is it possible to avoid it?

Example: http://jsfiddle.net/GqQKv/

var img = new Image();
img.src ='http://example.com/whatever?s=Hampshire 123&co=USA'
console.log('src', img.src); // instead of spaces we have %20

ThanksALot();

Emanuel Vintilă
  • 1,911
  • 4
  • 25
  • 35
Anelook
  • 1,277
  • 3
  • 17
  • 28
  • You know you can always decode it again? – RienNeVaPlu͢s May 26 '14 at 14:04
  • 3
    *All* URLs are "encoded" this way. [The character "space" (U+0020) is not valid in an URL.](http://stackoverflow.com/questions/497908/are-urls-allowed-to-have-a-space-in-them) – Jongware May 26 '14 at 14:07
  • Thanks Jongware, this is the answer I needed. – Anelook May 26 '14 at 14:11
  • I know that I can decode it after, but I wanted to know if it is possible to set 'src' parameter in a decoded way, because one of our services expects requests with clean text. – Anelook May 26 '14 at 14:13

1 Answers1

2

Just decode it using decodeURI(uri).

That would be console.log('src', decodeURI(img.src));.

Updated FIDDLE

Emanuel Vintilă
  • 1,911
  • 4
  • 25
  • 35
  • Sorry, but it was not the question I asked. – Anelook May 26 '14 at 14:14
  • Nobody forces you to accept an answer. I understand you did not ask this, I just provided an answer, without knowing what uses you have for this code. I just assumed you wanted to print the image's `src`, because that's what this code snippet does. – Emanuel Vintilă May 26 '14 at 14:17
  • PsioniaX, I'm sorry, I didn't mean to be rude. I just wanted to explain why I haven't accepted the answer. I was interested if I could _avoid_ automatic encoding of the src parameter. Probably should made myself more clear. – Anelook May 26 '14 at 14:20
  • I didn't say you were rude, but yes, you could've made yourself more clear. – Emanuel Vintilă May 26 '14 at 14:20