{"id":225,"date":"2025-04-01T11:24:49","date_gmt":"2025-04-01T11:24:49","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=225"},"modified":"2025-04-01T11:24:49","modified_gmt":"2025-04-01T11:24:49","slug":"tabbed-navigation-with-querystring-open-tab-selection","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/tabbed-navigation-with-querystring-open-tab-selection\/","title":{"rendered":"Tabbed Navigation With QueryString Open Tab Selection"},"content":{"rendered":"<p>The following code will create a series of tabbed sections on a single page. Each tabbed section will have a series of boxes that contain links to various websites.<\/p>\n<p>Set up the tabs and box containers:<\/p>\n<pre><code class=\"language-html line-numbers\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"UTF-8\"&gt;\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\r\n    &lt;title&gt;Tabbed Web Page&lt;\/title&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"styles.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"tabs\"&gt;\r\n    &lt;div id=\"buttoncontainer\"&gt;\r\n    &lt;button class=\"tablink active\" onclick=\"openTab(event, 'Tab1')\"&gt;Tab 1&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab2')\"&gt;Tab 2&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab3')\"&gt;Tab 3&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab4')\"&gt;Tab 4&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab5')\"&gt;Tab 5&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab6')\"&gt;Tab 6&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab7')\"&gt;Tab 7&lt;\/button&gt;\r\n    &lt;button class=\"tablink\" onclick=\"openTab(event, 'Tab8')\"&gt;Tab 8&lt;\/button&gt;\r\n&lt;\/div&gt;&lt;!--END ButtonContainer--&gt;\r\n\t&lt;div id=\"SearchForm\"&gt;\r\n\t\t&lt;form action=\"https:\/\/www.bing.com\/search\" method=\"get\" onsubmit=\"return AddressSearchCheck()\"&gt;\r\n\t\t&lt;input type=\"text\" name=\"q\" value=\"\" placeholder=\"Search Term\" id=\"SearchField\"&gt;&lt;input type=\"Submit\" value=\"Search\"&gt;\r\n\t\t&lt;\/form&gt;\r\n\t&lt;\/div&gt;&amp;lt!--END searchform--&gt;\r\n&lt;\/div&gt;&lt;!--END tabs--&gt;\r\n\r\n&lt;div id=\"Tab1\" class=\"tabcontent\"&gt;\r\n    &lt;div class=\"box-container\"&gt;\r\n        &lt;a href=\"https:\/\/www.example1.com\" class=\"box\"&gt;\r\n            &lt;img src=\"icon1.png\" alt=\"Icon 1\"&gt;\r\n        &lt;\/a&gt;\r\n        &lt;a href=\"https:\/\/www.example2.com\" class=\"box\"&gt;\r\n            &lt;img src=\"icon2.png\" alt=\"Icon 2\"&gt;\r\n        &lt;\/a&gt;\r\n        &lt;!-- Add more boxes as needed --&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div id=\"Tab2\" class=\"tabcontent\"&gt;\r\n    &lt;div class=\"box-container\"&gt;\r\n        &lt;a href=\"https:\/\/www.example3.com\" class=\"box\"&gt;\r\n            &lt;img src=\"icon3.png\" alt=\"Icon 3\"&gt;\r\n        &lt;\/a&gt;\r\n        &lt;a href=\"https:\/\/www.example4.com\" class=\"box\"&gt;\r\n            &lt;img src=\"icon4.png\" alt=\"Icon 4\"&gt;\r\n        &lt;\/a&gt;\r\n        &lt;!-- Add more boxes as needed --&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;!-- Repeat for other tabs --&gt;\r\n&lt;\/div&gt;<!--END Tab Content-->\r\n\t\t&lt;script src=\"script.js\"&gt;&lt;\/script&gt;\r\n\t&lt;\/body&gt;\r\n\t&lt;\/html&gt;\r\n\r\n\r\n<\/code><\/pre>\n<p>The following CSS (styles.css) will style the tabs and boxes:<\/p>\n<pre><code class=\"language-css line-numbers\">\r\nbody {\r\n    font-family: Arial, sans-serif;\r\n}\r\n\r\n.tabs {\r\n    display: flex;\r\n    justify-content: space-around;\r\n    margin-bottom: 20px;\r\n}\r\n\r\n.tablink {\r\n    padding: 10px 20px;\r\n    background-color: #f1f1f1;\r\n    border: none;\r\n    cursor: pointer;\r\n    transition: background-color 0.3s;\r\n}\r\n\r\n.tablink:hover {\r\n    background-color: #ddd;\r\n}\r\n\r\n\/* Style for the active tab *\/\r\n.tablink.active {\r\n    background-color: #4CAF50; \/* Change this to your desired active tab color *\/\r\n    color: white; \/* Optional: Change the text color of the active tab *\/\r\n}\r\n\r\n.tabcontent {\r\n    display: none;\r\n    padding: 20px;\r\n    border: 1px solid #ccc;\r\n}\r\n\r\n.tabcontent .box-container {\r\n    display: flex;\r\n    flex-wrap: wrap;\r\n    justify-content: flex-start; \/* Align boxes to the left *\/\r\n}\r\n\r\n.box {\r\n    width: 300px;\r\n    height: 300px;\r\n    margin: 15px;\r\n    padding: 20px;\r\n    border: 2px solid grey;\r\n    border-radius: 10px;\r\n    text-align: center;\r\n    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);\r\n    background-color: #fff;\r\n    display: flex;\r\n    align-items: center;\r\n    justify-content: center;\r\n    transition: transform 0.3s;\r\n    text-decoration: none; \/* Remove underline for the link *\/\r\n    color: inherit; \/* Inherit the text color *\/\r\n}\r\n\r\n.box img {\r\n    max-width: 100px;\r\n    max-height: 100px;\r\n    display: block;\r\n}\r\n\r\n.box:hover {\r\n    transform: scale(1.05);\r\n    border-color: #000; \/* Optional: change border color on hover *\/\r\n}\r\n\r\n@media (max-width: 768px) {\r\n    .box {\r\n        width: 100%;\r\n        margin: 10px 0;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The following JavaScript (script.js) will open the clicked tabbed, load the tab from the queryString<\/p>\n<pre><code class=\"language-html\">\r\n\/\/ Function to open a tab\r\nfunction openTab(evt, tabName) {\r\n    var i, tabcontent, tablinks;\r\n\r\n    \/\/ Hide all tab content\r\n    tabcontent = document.getElementsByClassName(\"tabcontent\");\r\n    for (i = 0; i &lt; tabcontent.length; i++) {\r\n        tabcontent[i].style.display = \"none\";\r\n    }\r\n\r\n    \/\/ Remove the active class from all tabs\r\n    tablinks = document.getElementsByClassName(\"tablinks\");\r\n    for (i = 0; i &lt; tablinks.length; i++) {\r\n        tablinks[i].className = tablinks[i].className.replace(\" active\", \"\");\r\n    }\r\n\r\n    \/\/ Show the current tab and add an active class to the clicked tab\r\n    document.getElementById(tabName).style.display = \"block\";\r\n    evt.currentTarget.className += \" active\";\r\n\t\r\n\tTabbyText = evt.currentTarget.innerHTML;\r\n\tdocument.getElementById(\"TabName\").innerHTML = TabbyText;\r\n\tdocument.title = TabbyText;\r\n}\r\n\r\n\/\/ Function to open a specific tab based on the query string\r\nfunction openTabFromQueryString() {\r\n    const urlParams = new URLSearchParams(window.location.search);\r\n    const tabName = urlParams.get('tab');\r\n    if (tabName) {\r\n        const tabButton = document.querySelector(`button[onclick=\"openTab(event, '${tabName}')\"]`);\r\n        if (tabButton) {\r\n            tabButton.click();\r\n        }\r\n    } else {\r\n        \/\/ Open the first tab by default if no query string is provided\r\n        document.querySelector('.tab button').click();\r\n    }\r\n}\r\n\r\n\/\/ Add tooltips to all boxes with a data-tooltip attribute\r\ndocument.querySelectorAll('.box').forEach(function(box) {\r\n    var tooltipText = box.getAttribute('data-tooltip');\r\n    \r\n    if (tooltipText) {\r\n        \/\/ Create the tooltip element\r\n        var tooltip = document.createElement('div');\r\n        tooltip.className = 'tooltip';\r\n        tooltip.textContent = tooltipText;\r\n        \r\n        \/\/ Append the tooltip to the box\r\n        box.appendChild(tooltip);\r\n    }\r\n});\r\n\r\nfunction AddressSearchCheck() {\r\n    \/\/ Get Search Field Contents\r\n    const searchField = document.getElementById(\"SearchField\");\r\n    let SearchText = searchField.value.trim();  \/\/ Trim whitespace\r\n\r\n    if (SearchText === '') {\r\n        alert('Enter some text you muppet');\r\n        return false;\r\n    } else {\r\n        \/\/ Normalize the text for easier comparison\r\n        let normalizedSearchText = SearchText.toLowerCase();\r\n\r\n        \/\/ Check if it contains '.co' (assuming it's a URL if it does)\r\n        const isURL = \/\\.(co|net|org)\/.test(normalizedSearchText);\r\n\r\n        if (isURL) {\r\n            \/\/ Check if it already starts with 'http:\/\/' or 'https:\/\/'\r\n            const isHTTPS = normalizedSearchText.startsWith('https:\/\/');\r\n            const isHTTP = normalizedSearchText.startsWith('http:\/\/');\r\n\r\n            \/\/ If the input doesn't start with 'http:\/\/' or 'https:\/\/', prepend 'https:\/\/'\r\n            if (!isHTTPS &amp;&amp; !isHTTP) {\r\n                normalizedSearchText = 'https:\/\/' + SearchText;\r\n            } else {\r\n                \/\/ Use the original text with the correct case\r\n                normalizedSearchText = SearchText;\r\n            }\r\n\r\n            \/\/ Redirect to the URL\r\n            window.location.href = normalizedSearchText;\r\n        } else {\r\n            \/\/ Handle search term (e.g., redirect to Google search or another search engine)\r\n            \/\/window.location.href = 'https:\/\/www.google.com\/search?q=' + encodeURIComponent(SearchText);\r\n\t\t\treturn true;\r\n        }\r\n\r\n        \/\/ Return false to stop further processing (optional)\r\n        return false;\r\n    }\r\n}\r\n\r\n\r\nwindow.onload = function() {\r\n    \/\/ Call the function to open the correct tab based on the query string\r\n    openTabFromQueryString();\r\n\r\n    \/\/ Focus on the search field\r\n    const searchField = document.getElementById(\"SearchField\");\r\n    if (searchField) {\r\n        searchField.focus();\r\n    }\r\n};\r\n\r\n\r\n\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code will create a series of tabbed sections on a single page. Each tabbed section will have a series of boxes that contain [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,46],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-html","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}