18

I would like to know if it's possible to change the contents of the URL in the browser without reloading the page?

I use jQuery and Ajax to load new parts of my page. When I choose "product one", the direct link would be mysite.com/product1 and for "product two" would be mysite.com/product2, but I don't want to reload the site to these pages.

jonsca
  • 10,218
  • 26
  • 54
  • 62
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168

7 Answers7

14

its now possible with HTML_5..

chack this link... http://www.spoiledmilk.dk/blog/?p=1922

also facebook and google using this tric beside Hash(#) attribute

Pavel
  • 149
  • 1
  • 2
13

You will have to add hash # if you want to prevent page from reloading.

The css-tricks.com has an excellent screencast on that, have a look at:

Best Practices with Dynamic Content

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
7

This is possible in HTML5. See a demo here.

You can change the URL to another URL within the same domain, but can not change the domain for security reasons.

See the history interface in HTML5 specification for details.

Sparky
  • 4,769
  • 8
  • 37
  • 52
7

just use this one

window.history.pushState("object or string", "Title", "/new-url");
naveen
  • 1,078
  • 1
  • 13
  • 26
  • Watch out for IE < 10: http://caniuse.com/#search=pushstate. Use a polyfill like history.js. – Johann Feb 24 '15 at 22:31
1

Yes, it is possible using the HTML5 History API. Check this page and this example

Adi
  • 5,089
  • 6
  • 33
  • 47
1

You CAN do that. Though likely you'll need a modern browser. Have a look at this page: http://www.20thingsilearned.com/ created by the Google Chrome team (I used Chrome 9 to read it). Changing pages doesn't reload the entire web page, but changes the URL.

Attila O.
  • 15,659
  • 11
  • 54
  • 84
-4

You can't. Only if you change the hash, like sAc told you.

But.. May I ask WHY?

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58
  • 2
    I'm not the OP, but I find it useful when you change your content via ajax, and then your visitor wants to store the page he is looking at to bookmarks/send via email/ etc.. – nothrow Aug 01 '10 at 08:43
  • Like Yossarian said, that was one of my reasons. Also because the load of the page loading is less using ajax, and the loading of contens is better looking. – Paul Peelen Aug 01 '10 at 19:43
  • @Yossarian: I asked why he wants to use site.com/product, like the initial Q was (not hash). @Paul: if you change more than 4-50% of your page content, you may want to rethink your strategy, because the speed you will gain with page transfer will be canceled by the DOM operation time :) – Ionuț Staicu Aug 02 '10 at 06:18
  • Wrong answer. In HTML5 it is posible to do that. – sasa Apr 03 '11 at 10:49
  • 1
    @Sasa, agree. But try to do that in IE9 :) – Ionuț Staicu Apr 07 '11 at 08:18