Link to home
Start Free TrialLog in
Avatar of DanielSKim
DanielSKim

asked on

Replacing Pound Sign in Window.Location

Simple example of my issue:

on page1.htm, I have a link:

<a href="page2.htm?do=listen#man">click</a>

what i am trying to do on page2.htm is to retrieve the window location url, assign it to a string, and then replace any pound signs in the string with double pound signs in the string.

this is what i've tried on page2.htm:

<script>

var newLocation = '';

newLocation = window.location;
alert(newLocation); // looks like the url is in the newLocation variable

newLocation = newLocation.replace(/\#/g,"");

alert(newLocation);

</script>

Any help would be welcome.
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanielSKim
DanielSKim

ASKER

%?@!. realized i should have been referencing the property.

thank you!
Yes, you have to refer to the href property. In your old code, you tried to replace the location object, which as you realized doesn't work:-)

Batalf
Avatar of Zvonko
By the way, your so called pound char is the hash character and is used to jump on page to the named anchor name following the hash char.
The named anchor name is provided by the window.location.hash property.

well, that's the issue. i'm receiving a url variable that has a pound sign in it, and the server side script is not seeing the entire variable, as it interprets anything after the pound sign. i want to replace the pound sign with something else, so that the entire variable is passed.
the sender of the variable containing the hash char has to urlencode() or to escape() the value before passing it to the url.
In JavaScript you need only then to unescape() the window.location.search to get the passed parameters.