View Full Version : Simple problem with location.href.replace

12-22-2005, 04:43 PM

I am facing a very simple problem, I have a URL like this,


Whenever user gets to this URL, the URL needs to change like this,


%2d should change to -
%7b should change to { &
%7d should change to }

I am trying it like this,

function InitPage()
location.href = location.href.replace('\%2d', "-");
location.href = location.href.replace('\%7b', "{");
location.href = location.href.replace('\%7d', "}");

<BODY marginwidth=0 marginheight=0 scroll="yes" onload="InitPage();">

With this the page keeps on refreshing continuosly... :confused:
Any help is verymuch appreciated... :thumbsup:

12-22-2005, 08:03 PM
thats because you cannot unescape those characters in the url . hence why they are encoded in unicode.

if you wish to return these values to read later run the replace after you return them - not in the url

whats happening:

location =

location.href = location.href.replace('\%2d', "-");

page reloads changing the characters %2d to simply '-'

then the browser takes over and converts the '-' character to %2d

now at this point your init script will hit again and replace the same characters in the url. - therefore reloading the document and starting the loop again.

i suggest

var url = location.href;
url = url.replace('\%2d,'-');
url = url.replace('\%7b,'{');
url = url.replace('\%7d,'}');

then work with the variable url.