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.
on page1.htm, I have a link:
<a href="page2.htm?do=listen#
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Batalf
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.
The named anchor name is provided by the window.location.hash property.
ASKER
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.
In JavaScript you need only then to unescape() the window.location.search to get the passed parameters.
ASKER
thank you!