Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap auto select tab on page load

I'm using Bootstrap V3 and am wondering if there is a way I can auto select a specific tab when the page loads. It sounds simple enough but nothing that I try which I expect would work....works.

If I navigate to this in a browser it works fine...(assuming my tab has an ID of tab_files)

WEBSITE_URL/contact.php?page=contact&token=XYZ#bs-tab-tab_files

But, if I were to call the above link from an a tag or javascript then it doesn't work.

Am I missing something?

like image 314
Rob Avatar asked Dec 30 '25 10:12

Rob


1 Answers

Sure you can. Check if there is a hash on the URL using window.location.hash and then call tab('show') on a link with that href attribute

if (window.location.hash) {
    $("a[href='" + window.location.hash + "']").tab('show');
}

You can take it a step further and make sure the item is actually a tab, but the above will suffice. Make sure that runs after your DOM is loaded.

like image 171
Josh Avatar answered Jan 01 '26 23:01

Josh