1

I have written a simple code in a .js file (in Zend framework, but I dont think it matters)

var location = "localhost:8080/mymodule/id/1#";
location.replace(/\#/g, "");
alert(location.valueOf());
return;

but I dont know why I can not see the result I want. I get my page url and want to omit all number signs appears in it. but the code above does nothing. please help

ttt
  • 11
  • 1

3 Answers3

6

location is a bad name to use for a variable since it collides with the window.location variable used for the actual browser page location.

If you change location to loc in your above code, and then also add loc = in front of the loc.replace() call (since replace() doesn't modify the input, but instead returns the new version), your code works.

Amber
  • 477,764
  • 81
  • 611
  • 541
6

replace will not change the value of the original string, you need to assign the result to a new variable -

var newString = location.replace(/#/g, "");
alert(newString);

Demo - http://jsfiddle.net/5H5uZ/

ipr101
  • 23,772
  • 7
  • 57
  • 61
2

It can be done in one line. This is the result you look for?

alert("localhost:8080/mymodule/id/1#".replace(/#/g,'')); 
  //=> alerts 'localhost:8080/mymodule/id/1'
KooiInc
  • 112,400
  • 31
  • 139
  • 174