question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow anchor links to automatically load JS tabs

See original GitHub issue

Currently it’s necessary to add a small junk of JS (created below) to activate NAV tabs/pills based on the anchor link provided on the tab/pill itself when a page is loaded. This is for deep linking to a tab/pill on a page. This feature request is to integrate this behaviour automatically as part of bootstrap v4 JS.

// Show appropriate pill based on #anchor in URL
var url = window.location.href;
if (url.indexOf("#") > 0){
var activeTab = url.substring(url.indexOf("#") + 1);
  $('.nav[role="tablist"] a[href="#'+activeTab+'"]').tab('show');
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:26
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
inwardmovementcommented, Sep 27, 2019

Here is a code that handles a) changing the url on tab click and b) switching to it:

// LINK TO TABS
$(document).ready(() => {
  var url = window.location.href;
  if (url.indexOf("#") > 0){
  var activeTab = url.substring(url.indexOf("#") + 1);
    $('.nav[role="tablist"] a[href="#'+activeTab+'"]').tab('show');
  }

  $('a[role="tab"]').on("click", function() {
    var newUrl;
    const hash = $(this).attr("href");
      newUrl = url.split("#")[0] + hash;
    history.replaceState(null, null, newUrl);
  });
});

Indeed it would be great if it was a default and/or switchable feature (via Sass variables).

5reactions
alainravetcommented, Apr 6, 2018

You can obtain the url anchor directly with window.location.hash

const anchor = window.location.hash;
$(`a[href="${anchor}"]`).tab('show')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to anchor link to tabs. How to have a link jump to an ...
So, "First uncheck all tabs and then check the only tab that I pressed". It is what the code says.
Read more >
Put anchor in tab and click link to switch to each tab
I'd like to ask if there is a way(css or code) to put an anchor in tab content ... I click on a...
Read more >
Accessible toggle tabs and accordions - Go Make Things
Start with simple anchor links and content sections with IDs. ... on the browser will now automatically jump you between tabs or accordions, ......
Read more >
Anchor link inside same tab - JavaScript - SitePoint Forums
All you need is to add a tablink class to any link that is inside a tab (links that open in the same...
Read more >
How to Use HTML to Open a Link in a New Tab - freeCodeCamp
To create a link on a web page, you need to wrap an element (text, a picture, and so on) in an anchor...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found