I was looking for a solution to have a certain tab open when page was viewed by Url Parameter. I have seen a few examples but they do not seem to work. Maybe they are to old to work with current javascript. Here is what I have found:
var tabName = (window.location.href.match(/[?&]tab-name=[^&$]+/i) || '=').split('=')[1];
tabName='profile'; // remove this line on real page
if(tabName.length)
$('#myTabs .nav-link[href="#' + tabName + '"]').tab('show');
The above works fine with the // remove this line on real page - in place. once that is taken out and it relies on the varible. Console window says (window.location.href.match(/[?&]tab-name=[^&$]+/i) || '=').split('=')[1]; is not a correct function.
I have this and it does not seem to work either. It gets the parameter but it still needs stripped to get just the parameter to open the tab.
$(document).ready(function () {
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
var tabName = params;
if (tabName.length)
$('#myTabs .nav-link[href="#' + tabName + '"]').tab('show');
debugger
});
Any help would be appreciated..
I got this to work by doing it this way..
$(document).ready(function () {
let params = new URLSearchParams(document.location.search.substring(1));
let name = params.get("name");
if (name.length)
$('#myTabs .nav-link[href="#' + name + '"]').tab('show');
//debugger
});
URL is myAddress.com/Profile?name=settings
Weird, when I used the word tabName this didn't work but name does..