Vesica Blog Taking museums and art collections to the cloud

January 21, 2013

Tech Talk: URL Hashing and JQuery UI Tabs

Whilst Vesica uses JQuery, what’s discussed in this article is not used in Vesica, but in a similar application we helped build at the  NHS (National Health Service) which borrows from the Vesica interface.

The application in question is a single page application, so virtually everything is done via AJAX. The URL receives different parameters via a URL hash, which updates different sections of the page DOM or accesses different parts of the interface, including JQuery UI tabs. These single page, multiple hash URLs can be called via a bookmark URL, or will trigger on a url change in the browser. What’s important to note here is that in an application like this, when you submit data, all you really need to do is change the URL – your hashing managing JavaScript should do the rest for you (in terms of routing your data to the server side, not actual processing). I’ll also show you some JavaScript to deal with JQuery UI tabs after your page loads as working with hashing will disable the default URL behaviour that allows you to call tabs via the URL.


If it’s not self-explanatory, you need the following to accomplish this:

  • JQuery -
  • JQuery UI -
  • JQuery Hashchange plugin from Ben Alman - and
Please load the JavaScript libraries in your page (preferably in the header) in the order listed.

How it works

Here is what we need to cover:

  1. Someone either loads a URL in their browser which is part of your single page application (this could be via a bookmarked URL or by simply typing or pasting in the address bar), in which case you simply need to pick up the URL, process, and update the page.
  2. Someone performs an action that will change the URL of your application – which should either update a database or update the page, or whatever it is supposed to do in your application.
The JQuery Haschange plugin will only help you accomplish number 2. Number 1 is simple JavaScript / JQuery.
So how does your application know that the URL in the browser has changed after the #? With the following code.

So if your existing URL looked like and you changed it to with an <a href> or a <button>, would see the alert box above. Note that if you wanted to to alert something anyway, you would simply put that code outside the $(window).hashchange() function.

This is the easy bit – now for how you would pick up different parts of your URL, splice them up and send them to your scripts as needed to process, and then perhaps load a JQuery UI tab on your page.


Let’s clarify what we have in our example.

We are trying to load a different type of product based on a type parameter, followed by an ID, followed by a bunch of parameters that style the image and options that get displayed for the product.

So, if you were using PHP and building your URL query string, it might look a bit like:

If you wanted to load a particular JQuery tab on this page that had the id ‘dimensions’, your URL would become:

When the page loaded, JQuery UI would know you want to load the dimensions tab and it would simply make it active.

But using the $(window).hashchange() function above will break this default JQuery UI functionality.

So, our goal, given our single page application, is to be able to reload a certain part of our page based on a URL that looks like the following:

The way to read the above your URL is: Give me the product page with bottle No. 3426 in the colour green and display the dimensions tab on the product page. You could change any of the values above to process different data – so you could have product, category or generic types of pages, hundreds of different products and hundreds of different colours.

Sounds simple enough. This would be accomplished with the following code, and please note, as mentioned above, that to trigger this change for bookmarked URLs or those pasted in the browser you can enter this code outside the $(window).hashchange() function in a <script type=”text/JavaScript”></script> tag directly in index.php (where index.php is the file that runs your single page application) or create a another JavaScript file and include it in your index.php file via the <script> tag. To trigger it with a button click or <a href> change, please wrap it inside the $(window).hash function as shown below. Technically, you can wrap all this code in a function and call that on $(window).hashchange() being executed.

The above code now gives you access to to all the different parts of the URL hash, and will do so every time it changes. All you need to do now is use the $.ajax() function in JQuery to process this data, get the results back, and set the active tab as shown below:

That’s it, that code should pick up any changes to the URL and process them according to the code above.

As this is JQuery, remember to wrap all of the above code in $(function(){ {); as shown below. I’ve also added the code above outside the hashchange function so you can see how to setup the default page handler as well as trigger it off when the page URL changes.

I’ve stated before that you can (and probably should) write a function to do the above so you don’t have to reproduce your code twice – but this should give you an idea of how to handle hash changes and process them.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Home    •    Blog    •    Contact Us    •    Developers    •    Education    •    Partners    •    About    •    Help & Support    •    News    •    Privacy Policy    •    Terms of Use

Follow us on Twitterk Join Vesica on Facebook