Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 664
  • Last Modified:

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.
0
DanielSKim
Asked:
DanielSKim
  • 2
  • 2
  • 2
1 Solution
 
BatalfCommented:
Try

<script>

var newLocation = '';

newLocation = window.location.href;


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

alert(newLocation);

</script>
0
 
DanielSKimAuthor Commented:
%?@!. realized i should have been referencing the property.

thank you!
0
 
BatalfCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ZvonkoSystems architectCommented:
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.

0
 
DanielSKimAuthor Commented:
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.
0
 
ZvonkoSystems architectCommented:
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.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now