0

I have noticed in quite few websites like soundcloud where when you click on a link, the page loads like it was there only something like ajax, but still the address changes in address bar.

For example, when I am exploring sounds on soundcloud, clicking on a link, opens the page but the background music keeps on playing. If its ajax, then how do the address changes in address bar.

I am newbie web developer, want to learn this technique!!!

Suyash Mohan
  • 310
  • 3
  • 10

3 Answers3

1

Check out history.pushState.

HTML5-friendly browsers supports new history apis named pushState/replaceState that enables javascript to take control of browser history without reload the page. In older browser, features like location.hash is usually used.

kyriosli
  • 333
  • 1
  • 6
0

Have a look at this question's answer https://stackoverflow.com/a/4059844/1558255

In it you can see examples for the use of window.history in HTML5 such as the following from Alfred Nerstu's answer.

  window.history.pushState('Object', 'Title', '/new-url');

If you just want to change the url without being able to go back

  window.history.replaceState('Object', 'Title', '/another-new-url');

The object can be used for ajax navigation:

  window.history.pushState({ id: 35 }, 'Viewing item #35', '/item/35');

  window.onpopstate = function (e) {
     var id = e.state.id;
     load_item(id);
  };
Community
  • 1
  • 1
Klors
  • 2,665
  • 2
  • 25
  • 42