Question:
JAvascript help how to remove %20 from auto generated url?
Tau
2009-09-17 22:46:25 UTC
script
a href="http://itunes.com/{$rel.album_artist_name}/{$rel.album_title}


Takes the artist name and album title from a database and generates an itunes like eg http://www.itunes.com/djtau

If the artist name and the album names haves spaces the link eg "dj tau" generates as http://www.itunes.com/dj%20tau which is incorrect

how do i add a replace delete white space string into my script like:-

string = string.replace(new RegExp(/^\s+/),"");

im not a programmer need help :(
Four answers:
hotzelj
2009-09-17 22:56:39 UTC
There should be a sun.* package I believe that has a URL decoder class in it that will accept a URL-encoded string and output just the text (no encoding).



http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLDecoder.html
anonymous
2009-09-18 06:59:49 UTC
you can`t, white space will always be replace by %20, because it is a code that represents a white space.



what you can do is, when you retrieve the information from the database, use regular expression to check if there is white space, and remove it.



If you seriously need a white space, replace the whitespace with underscore ( _ )
McQuack
2009-09-18 06:31:40 UTC
decodeURI(URIstring) is a native function in javascript that remove the url encoding you are seeing, EX %20. You could use a regualar expresion to correct it but using the native function is much easier.



in your case it would probably look like this



string = decodeURI(string);



Some reference can be found at this site both a encode and decode are available natively.



http://www.w3schools.com/jsref/jsref_decodeURI.asp
jesusvnuys
2009-09-18 05:54:27 UTC
%20 is just a different way to write a "space"

an URL cannot have spaces so the browser adds the %20 and not the javascript ;)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...