views:

146

answers:

3

Hi! I just wanted to know, is there any possible way to change the URL that appears in the address bar of a webpage dynamically? Like, maybe there are two buttons on the webpage, and when the user clicks one it will (or won't it does not matter) refresh the page and the url will be mysite.com/page1, or if the user clickes the second button, the url that appears in the address bar will be mysite.com/page2?

I do not need it to chaneg the domain, just the part after.

Thanks to anyone who helps!

Just wanted to say, that I DO NOT want to go to another page. This must be done on one page. It does not matter if it is done with JS, PHP, or via the .htaccess file, but it must do this with only one page.

+3  A: 

Outside of changing the .location you only really have control over the window.location.hash.

window.location.hash = "boo"; http://mysite.com -> http://mysite.com/#boo

This is the only way to not go to a new page while changing the URL. All other methods will refresh the page or redirect the page:

  1. window.location redirects user when changed
  2. window.location.pathname redirects user when changed
  3. window.location.search redirects user when changed
  4. window.location.hash does not redirect user when changed

You can also just change the non domain path by using a relative url:

window.location = "page1"; // include forward-slash if necessary
                           // goes to http://somesite.com/page1
Jonathan Sampson
Jonathan you are one stackoverflowin sonofagun!
jeerose
Thanks, I think :) Hehe.
Jonathan Sampson
I'm definitely thankful for your contribution.
jeerose
I appreciate your kindness, jeerose. Thank you for contributing as well!
Jonathan Sampson
+1  A: 

You can definitely (and easily) serve the same page off both /page1 and /page2 and have the buttons navigate respectively to one and the other -- "refreshing the page", as you say (i.e. loading it up again from the server, or browser cache), and of course change accordingly what appears in the address bar, too. However, I don't see what's the point of doing that.

Alex Martelli
+1  A: 

I don't quite understand what you want.

Is it that in both cases, you want the same page to be shown but with different urls ?

In that case, you could write a .htaccess file to redirect to the same page for both /link1 and /link2 and point the button to either of the links.

_sh