'Make Bootstrap tab Active on the bases of URL link
I have bootstrap tabs and i want to make tabs active on the bases of URL link.
For example:
xyz.xxx/index#tab2 should make tab2 active on page load.
Here is my code so far
<div class="tab-wrap">
<div class="parrent pull-left">
<ul class="nav nav-tabs nav-stacked">
<li class="active"><a href="#tab1" data-toggle="tab" class="analistic-01">Tab 1</a></li>
<li class=""><a href="#tab2" data-toggle="tab" class="analistic-02">Tab 2</a></li>
<li class=""><a href="#tab3" data-toggle="tab" class="analistic-03">Tab 3</a></li>
</ul>
</div>
<div class="tab-content">
<div class="tab-pane active in" id="tab1">
<p> hello 1</p>
</div>
<div class="tab-pane" id="tab2">
<p> hello 2</p>
</div>
<div class="tab-pane" id="tab3">
<p>Hello 3</p>
</div>
</div> <!--/.tab-content-->
</div><!--/.tab-wrap-->
By default it make tab1 active so there might be possibility in my case to make the second tab active by default on the basis of my URL links.
Suppose if i put #tab2 in URL then it should make tab2 active by default on page load.
Solution 1:[1]
- Get the hash from URL and display active tab
- Set the current hash to the URL
You can use this code:
$(function(){
var hash = window.location.hash;
hash && $('ul.nav.nav-pills a[href="' + hash + '"]').tab('show');
$('ul.nav.nav-pills a').click(function (e) {
$(this).tab('show');
$('body').scrollTop();
window.location.hash = this.hash;
});
});
Solution 2:[2]
Depending on how you have set up your JS you should be able to do this:
www.myurl.com/page#tab1
www.myurl.com/page#tab2
www.myurl.com/page#tab3
Where #tab1, #tab2 and #tab3 are equal to the id of the tab
EDIT
Solution 3:[3]
You can add an id to <ul class="nav nav-tabs nav-stacked" id="myTab"> then use the following javascript code:
$(document).ready(() => {
let url = location.href.replace(/\/$/, "");
if (location.hash) {
const hash = url.split("#");
$('#myTab a[href="#'+hash[1]+'"]').tab("show");
url = location.href.replace(/\/#/, "#");
history.replaceState(null, null, url);
setTimeout(() => {
$(window).scrollTop(0);
}, 400);
}
$('a[data-toggle="tab"]').on("click", function() {
let newUrl;
const hash = $(this).attr("href");
if(hash == "#home") {
newUrl = url.split("#")[0];
} else {`enter code here`
newUrl = url.split("#")[0] + hash;
}
newUrl += "/";
history.replaceState(null, null, newUrl);
});
});
For more details follow this link.
Solution 4:[4]
If you just want to trigger it based on url, the you can also do as follow
$(function(){
var hash = window.location.hash;
if(hash != ''){
$("a[href='"+url+"']").click();
}
})
Solution 5:[5]
$(function(){
var url = window.location.href;
var activeTab = url.substring(url.indexOf("#") + 1);
if($.trim(activeTab) == 'Exhibitor') // check hash tag name for prevent error
{
$(".tab-pane").removeClass("active in");
$("#" + activeTab).addClass("active in");
$('a[href="#'+ activeTab +'"]').tab('show');
}
});
Solution 6:[6]
Using the URL() constructor from the URL API:
URL {
href: "https://stackoverflow.com/posts/67036862/edit",
origin: "https://stackoverflow.com",
protocol: "https:",
username: "",
password: "",
host: "stackoverflow.com",
hostname: "stackoverflow.com",
port: "",
pathname: "/posts/67036862/edit",
search: ""
}
Make Bootstrap tab Active on the bases of URL link:
$(function() {
/**
* Make Bootstrap tab Active on the bases of URL link
*/
const loc = new URL(window.location.href) || null
if (loc !== null) {
console.debug(loc)
if (loc.hash !== '') {
$('.tab-pane').removeClass('active in')
$(loc.hash).addClass('active in')
$(`a[href="${ loc.hash }"]`).tab('show')
}
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John R Perry |
| Solution 2 | Community |
| Solution 3 | Tim Kariuki |
| Solution 4 | Swapnil Ghone |
| Solution 5 | Ali Ahmad Pasa |
| Solution 6 |
