I am trying to open specific bootstrap tab from clicking on an external link.
<ul class="nav nav-tabs">
<li><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#message" data-toggle="tab">Messages</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="profile">Contents</div>
<div class="tab-pane" id="message">Contents</div>
</div>
// Out side link which is place on different page:
<a href="index.php?p=edit-profile#profile">Profile Link From Outside</a>
This is how I tried it using jquery:
// 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;
})
This is opening my tabs correctly. But my problem is when it open page scroll to the top of the page. Is there a way to avoid from this problem?
I tied it with .scrollTop();
but I couldn't figure it out.
After adding .scrollTop();
my code lookk like this.
// 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;
$('html,body').scrollTop(300);
});
Any idea would be highly appreciated. Thank you.