I am using the following code to separate content on a website and it is working fine.
However, if I wanted to link straight to a specific tab from another page how can I go about doing this?
Using the regular way with an ID in the url doesn't seem to work - e.g. mypage.html#a
Thanks in advance
I am using the following code to separate content on a website http://www.bootply./74926 and it is working fine.
However, if I wanted to link straight to a specific tab from another page how can I go about doing this?
Using the regular way with an ID in the url doesn't seem to work - e.g. mypage.html#a
Thanks in advance
Share Improve this question edited Aug 11, 2016 at 22:32 Tom el Safadi 6,8067 gold badges55 silver badges119 bronze badges asked Aug 11, 2016 at 22:18 tinOfBeanstinOfBeans 7371 gold badge9 silver badges22 bronze badges 1- Hey, did it work? If yes could you please accept my answer? – Tom el Safadi Commented Sep 1, 2016 at 18:06
2 Answers
Reset to default 6Although I do not have the chance to try it, I think you can do it via javascript.
When you are ming from another page to ex. mypage#abc
, #abc
can be retrieved by window.location.hash
. So, a simple solution could be this javascript:
$(document).ready(function(){
// get the tab from url
var hash = window.location.hash;
// if a hash is present (when you e to this page)
if (hash !='') {
// show the tab
$('.nav-tabs a[href="' + hash + '"]').tab('show');
}
});
Just make sure that the tabs you need to link (from other pages) have ids same as your hashes
The Advantage with this code is that you can navigate to those tabs from other sites as well not only within the same page.
$('#tab1 a').click(function (e) {
e.preventDefault();
$('a[href="' + $(this).attr('href') + '"]').tab('show');
})
You can see it working in my JSFiddle below:
http://jsfiddle/Anokrize/nP2Zk/317/