Home > Web Front-end > JS Tutorial > How to Navigate to a Specific Bootstrap Tab Using an External Link?

How to Navigate to a Specific Bootstrap Tab Using an External Link?

Barbara Streisand
Release: 2024-11-28 21:00:14
Original
850 people have browsed it

How to Navigate to a Specific Bootstrap Tab Using an External Link?

How to Navigate to a Specific Twitter Bootstrap Tab from an External Hyperlink

Twitter Bootstrap Tabs provide a convenient way to organize content into separate sections. However, accessing a specific tab from an external link can be problematic. This article will address this issue, guiding you through a solution that allows direct navigation to any desired tab.

Problem Statement

Consider the following HTML structure:

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

When clicked from an external page, these links should navigate to the specified tabs ("Home" and "Notes" respectively).

Solution

Utilize JavaScript to achieve this functionality:

// Enable tab navigation from links
var hash = location.hash.replace(/^#/, '');
if (hash) {
    $('.nav-tabs a[href="#' + hash + '"]').tab('show');
}

// Update hash on tab activation
$('.nav-tabs a').on('shown.bs.tab', function (e) {
    window.location.hash = e.target.hash;
});
Copy after login

Explanation

The shown.bs.tab event handler is triggered when a tab is activated. Within this handler, we update the browser's hash to reflect the currently active tab. This ensures that the hash is updated correctly when the tab is navigated through JavaScript, allowing users to access the desired tab upon reloading the page.

The above is the detailed content of How to Navigate to a Specific Bootstrap Tab Using an External Link?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template