Simon
Simon

Reputation: 1733

Access correct Tab using URL

I'm using Joshua Gatcke's 99Lime HTML Kickstart framework for prototyping. It uses an implementation of the jQuery tabs and I was wondering if it's possible to access a tab directly by the URL.

So for example, I have a page, with in this case, static content. One is #settings and another is #users. I want to redirect a user to /dashboard#users and have the users tab display immediately.

Is this something that's possible?

Upvotes: 0

Views: 692

Answers (2)

Florian Margaine
Florian Margaine

Reputation: 60747

Yep, it is. Have you tried anything? Or are you just asking if there is some out-of-the-box way to do this?

In case it is the first, here is some pseudo code to do this (I guess clicking on a tab displays it, right?):

window.onhashchange = function(e) {
    By.id(e.newUrl).click()
}

PS: using the By micro-library.

Upvotes: 1

vzwick
vzwick

Reputation: 11044

Voilá:

$(document).ready(function(){
    $(window).bind('hashchange', function(){
        $('ul.tabs a[href^="' + document.location.hash + '"]').click();
    });

    if (document.location.hash.length) {
        $(window).trigger('hashchange');            
    }
})​;​

Working fiddle

Edit:

Upon reading your question thoroughly, I realized that this is all you need:

$(document).ready(function(){
    if (document.location.hash.length) {
        $('ul.tabs a[href^="' + document.location.hash + '"]').click();
    }
})​;​

Upvotes: 3

Related Questions