2

I make a ajax request with .load. It is working good. But i have a problem with the url bar. I want change the url bar. For example. When the ajax loads about/contact page. I want to change the url bar to about/contact.

I used this.

window.location.replace = about/contact;

But now, he redirect direct to this page. But I do not want, that the page is redirect to that url. How can i fix that?

Thanks!

Mike Vierwind
  • 1,482
  • 4
  • 23
  • 44
  • You can not do that. However, you could add an anchor to the URL which does not reload the page, e.g. `www.example.com#about` – Uooo Sep 19 '12 at 07:13
  • See this answer: http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page – Jeff Sep 19 '12 at 07:14
  • @w4rumy [Are you sure about that?](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history). CNTRL + F **Adding and modifying history entries** – Ohgodwhy Sep 19 '12 at 07:18

2 Answers2

5

You can do this with the History API, though support is not universal yet, though good in browsers other than IE. For an example of it in use: http://html5demos.com/history. It will modify the visible URL as you want, and will also allow you to use the back button to go back to previous states.

Suggest you use something like history.js to this as it behaves accordingly for browsers that do not yet support the history API. It will use the History API first, and if that isn't present will use the hash technique.

If you want to do-it-yourself, then definately look up on the History API and how it works, here's some places to start: article 1, article 2

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
  • 1
    I would highly suggest adding [This link](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history). Which requires no API and is supported amongst many modern browsers. – Ohgodwhy Sep 19 '12 at 07:19
  • Good link @Ohgodwhy, have added it. The main reason I suggest history.js is because of how it acts as a sort of polyfill for browsers with no support – Josh Davenport-Smith Sep 19 '12 at 07:23
2

Use the ajax history plugin, like jquery-address.

Look at this : https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin

Community
  • 1
  • 1
andyf
  • 3,262
  • 3
  • 23
  • 37