"http://something.com:6688/remote/17/26/172"
if I have the value 172 and I need to change the url to 175
"http://something.com:6688/remote/17/26/175"
how can i do that in JavaScript?
"http://something.com:6688/remote/17/26/172"
if I have the value 172 and I need to change the url to 175
"http://something.com:6688/remote/17/26/175"
how can i do that in JavaScript?
Depends on what you want to do.
Actually change browser URL:
If you actually want to push the browser to another URL you'll have to use window.location = 'http://example.com/175'
.
Change browser URL hash
If you just want to change the hash you can simply use window.location.hash
.
Re-use the URL on links or similar
If you want to reference a URL in links or similar, look into @cjavapro answer.
var url = "http://something.com:6688/remote/17/26/172"
url = url.replace(/\/[^\/]*$/, '/175')
Split the String by /
then change the last part and rejoin by /
:
var newnumber = 175;
var url = "http://something.com:6688/remote/17/26/172";
var segements = url.split("/");
segements[segements.length - 1] = "" + newnumber;
var newurl = segements.join("/");
alert(newurl);
You can't actually change the URL that is shown in the address bar with javascript without a page refresh. If this was possible it would represent a fairly substantial security issue in my opinion. If you can live with a page refresh, window.location
is your best option.
If you are using jQuery, the jQuery BBQ plugin is pretty neat. It handles a lot of the hard work for you.