Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink

mraliks picture mraliks · Oct 22, 2011 · Viewed 290.4k times · Source

I'm developing a web page in which I'm using Twitter's Bootstrap Framework and their Bootstrap Tabs JS. It works great except for a few minor issues, one of which is I do not know how go directly to a specific tab from an external link. For example:

<a href="facility.php#home">Home</a>
<a href="facility.php#notes">Notes</a>

should go to the Home tab and the Notes tab respectively when clicked on the links from an external page

Answer

dubbe picture dubbe · Feb 22, 2012

Here is my solution to the problem, a bit late perhaps. But it could maybe help others:

// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
    $('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
} 

// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
    window.location.hash = e.target.hash;
})