Veerasundaravel's Ruby on Rails Weblog

December 2, 2011

Change browser URL without reloading the page – jQuery – HTML5

Filed under: jQuery, Ruby On Rails — Tags: , , , , — Veerasundaravel @ 4:14 pm

The traditional way of changing the browser url is just adding hash to the end of current URL like window.location = “#q=test&sort=date”;

But if you want to change complete url like window.location = “/profile/show/1”; will redirect the user to that page but it will reload page. So how we can change browser URL with reloading  the page. The better solution is HTML5 History API. More ever the jQuery plugin History.js provides lot easier configuration and options to achieve this.


History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it will revert back to using the old onhashchange functionality.

Download & Installation:

  1. Download History.js and upload it to your webserver. Download links: tar.gz or zip
  2. Include History.js
    • For jQueryv1.3+
      <script src=""></script>
    • For Mootoolsv1.3+
      <script src=""></script>
    • For Right.jsv2.2+
      <script src=""></script>
    • For Zeptov0.5+
      <script src=""></script>
    • For everything else
      <script src=""></script>

Note: If you want to only support HTML5 Browsers and not HTML4 Browsers (so no hash fallback support) then just change the /html4+html5/ part in the urls to just /html5/. Why supporting HTML4 browsers could be either good or bad based on my app’s use cases

Working with History.js:

once after including the javascript you can change the browser URL just by calling any one the js statement.

History.pushState({state:1}, “State 1”, “?state=1”); (or) History.replaceState({state:3}, “State 3”, “?state=3”)


    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        History.log(, State.title, State.url);

    // Change our States
    History.pushState({state:1}, "State 1", "?state=1"); // logs {state:1}, "State 1", "?state=1"
    History.pushState({state:2}, "State 2", "?state=2"); // logs {state:2}, "State 2", "?state=2"
    History.replaceState({state:3}, "State 3", "?state=3"); // logs {state:3}, "State 3", "?state=3"
    History.pushState(null, null, "?state=4"); // logs {}, '', "?state=4"
    History.back(); // logs {state:3}, "State 3", "?state=3"
    History.back(); // logs {state:1}, "State 1", "?state=1"
    History.back(); // logs {}, "Home Page", "?"
    History.go(2); // logs {state:3}, "State 3", "?state=3"


How would the above operations look in a HTML5 Browser?


Note: These urls also work in HTML4 browsers and Search Engines. So no need for the hashbang (#!) fragment-identifier that google “recommends”.

How would they look in a HTML4 Browser?


For further detailed documentation and queries you can refer :


  1. Thanks Veera, good tip

    Comment by Maurice — December 2, 2011 @ 10:47 pm

  2. Good script.
    I’ve implemented only for HTML5 supported browsers :
    Will try to add it for non supported browsers also.

    Comment by Arun David — February 22, 2012 @ 6:45 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: