Open Tab Pane regarding Url in javascript

200 Views Asked by At

Different topics are covered on the individual tabs. On the one are shown pictures on the next the menu card, etc..

I would now like to be able to select a specific tab with the url.

Example:
https://example.com/mypage -> Opens the page and the first tab.

https://example.com/mypage/tab2 -> Opens the page and the second tab.
or
https://example.com/mypage#tab2 -> Opens the page and the second tab.

This is my current code that I would like to modify:

document.getElementById("defaultOpen").click();

function openPane(evt, paneName) {
    // Declare all variables
    var i, tabpane, tablink;
  
    // Get all elements with class="tabpane" and hide them
    tabpane = document.getElementsByClassName("tabpane");
    for (i = 0; i < tabpane.length; i++) {
      tabpane[i].style.display = "none";
    }
  
    // Get all elements with class="s" and remove the class "active"
    tablink = document.getElementsByClassName("tablink");
    for (i = 0; i < tablink.length; i++) {
      tablink[i].className = tablink[i].className.replace(" active", "");
    }
  
    // Show the current tab, and add an "active" class to the button that opened the tab
    document.getElementById(paneName).style.display = "block";
    evt.currentTarget.className += " active";
  }
/* --- TABS HEADER & WRAPPER START --- */

.tabs {
  width: 100%;
  margin-top: 40px;
}

/* --- profile tab wrapper --- */

.profile-tab-wrapper {
  position: static;
  top: 0px;
  z-index: 999999999;
  display: flex;
  width: 100%;
  margin-top: 0px;
  padding-top: 0px;
  padding-bottom: 10px;
  clear: none;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  border-top: 1px solid #d1d1d1;
  background-color: transparent;
}

/* --- tab link --- */

.tablink {
  position: static;
  display: flex;
  margin-top: -1px;
  border-top: 1px solid #d1d1d1;
  border-right: none;
  border-left: none;
  border-bottom: none;
  margin-right: 20px;
  margin-left: 20px;
  padding-top: 10px;
  padding-right: 0px;
  padding-left: 0px;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  background: none;
  font-family: Montserrat, sans-serif;
  color: #d1d1d1;
  font-size: 13px;
  line-height: 13px;
  font-weight: 500;
  text-transform: uppercase;
  transition: 0.3s;
  cursor: pointer;
  outline: inherit;
}

.tablink.active {
  position: static;
  height: auto;
  margin-top: -1px;
  border-top: 1px solid #101010;
  background-color: transparent;
  font-family: Montserrat, sans-serif;
  color: #101010;
  font-size: 13px;
  line-height: 13px;
  font-weight: 500;
  text-transform: uppercase;
}

/* --- TABS HEADER & WRAPPER END --- */
/* --- TABS CONTENT WRAPPER START --- */

.content-wrapper {
  margin-top: 20px;
  width: 100%;
}

/* --- TABS CONTENT WRAPPER END --- */
/* --- TABS DISPLAY ON/OFF START --- */

.tabpane {
  display: none;
  animation: fadeEffect 1s;
}

/* --- TABS DISPLAY ON/OFF END --- */
/* --- LINKS TAB START --- */

/* --- titel --- */

.titel-wrapper {
  position: static;
  top: 0px;
  z-index: 999999;
  display: flex;
  width: 100%;
  margin-top: 20px;
  padding-top: 5px;
  padding-bottom: 5px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: stretch;
  -webkit-align-items: stretch;
  -ms-flex-align: stretch;
  align-items: stretch;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  background-color: hsla(0, 0%, 100%, 0.8);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

.titel {
  font-family: Montserrat, sans-serif;
  font-size: 18px;
  line-height: 22px;
  font-weight: 600;
  text-align: center;
  text-transform: uppercase;
}

/* --- button --- */

.button-wrapper {
  position: static;
  top: 0px;
  z-index: 999999;
  display: flex;
  width: 100%;
  margin-top: 0px;
  padding-top: 5px;
  padding-bottom: 0px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: stretch;
  -webkit-align-items: stretch;
  -ms-flex-align: stretch;
  align-items: stretch;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 auto;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  background-color: hsla(0, 0%, 100%, 0.8);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
  text-decoration: none;
}

.button {
  padding-top: 15px;
  padding-bottom: 14px;
  border-style: solid;
  border-width: 1px;
  border-color: #101010;
  border-radius: 5px;
  background-color: #101010;
  outline-color: #101010;
  outline-offset: 0px;
  outline-style: none;
  outline-width: 1px;
  -webkit-transition: color 200ms linear, background-color 800ms cubic-bezier(.23, 1, .32, 1);
  transition: color 200ms linear, background-color 800ms cubic-bezier(.23, 1, .32, 1);
  font-family: Montserrat, sans-serif;
  color: #fff;
  font-size: 14px;
  line-height: 19px;
  text-align: center;
  text-decoration: none;
}

.button:hover {
  background-color: rgba(16, 16, 16, 0);
  color: #101010;
}

/* --- LINKS TAB END --- */
/* --- ABOUT TAB START --- */

.about-left {
  display: flex;
  margin-top: 10px;
  padding-top: 0px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: start;
  -webkit-justify-content: flex-start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: start;
  -webkit-align-items: flex-start;
  -ms-flex-align: start;
  align-items: flex-start;
}

.about-center {
  display: flex;
  margin-top: 10px;
  padding-top: 0px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: start;
  -webkit-justify-content: flex-start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}

.about-right {
  display: flex;
  margin-top: 10px;
  padding-top: 0px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: start;
  -webkit-justify-content: flex-start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: end;
  -webkit-align-items: flex-end;
  -ms-flex-align: end;
  align-items: flex-end;
}

.about-titel {
  margin-top: 0px;
  padding-top: 0px;
  padding-bottom: 0px;
  font-family: Montserrat, sans-serif;
  font-size: 14px;
  line-height: 19px;
  font-weight: 600;
  text-align: center;
  text-transform: none;
}

.about-text {
  margin-top: 0px;
  margin-bottom: 0px;
  padding-top: 0px;
  padding-bottom: 0px;
  font-family: Montserrat, sans-serif;
  font-size: 12px;
  line-height: 14px;
  font-weight: 400;
  text-align: center;
  text-transform: none;
}

/* --- ABOUT TAB END --- */
/* --- NO CONTENT START --- */

.nocontent-header {
  margin-top: 20%;
  padding-top: 1px;
  padding-bottom: 1px;
  font-family: Montserrat, sans-serif;
  font-size: 12px;
  line-height: 12px;
  font-weight: 600;
  text-align: center;
  text-transform: uppercase;
}

.nocontent-subhead {
  margin-top: 0px;
  margin-bottom: 0px;
  padding-top: 1px;
  padding-bottom: 1px;
  font-family: Montserrat, sans-serif;
  font-size: 12px;
  line-height: 14px;
  font-weight: 300;
  text-align: center;
  text-transform: none;
}

/* --- NO CONTENT END --- */
<div class="tabs">

        <div class="profile-tab-wrapper">

          <button class="tablink active" onclick="openPane(event, 'Links')" id="defaultOpen">LINKS</button>

          <button class="tablink" onclick="openPane(event, 'Images')">IMAGES</button>


          <button class="tablink" onclick="openPane(event, 'About')">ABOUT</button>

        </div>


        <div class="tabs-content">
          
          <div id="Links" class="tabpane">
            <div class="content-wrapper">

              <div class="titel-wrapper">
                <div class="titel">Titel 1</div> <!-- Insert Titel -->
              </div>

              <div class="button-wrapper">
                <a class="button" href="https://example.com" target="_blank">
                  <div>Link 1</div>
                </a>
              </div>

            </div>
          </div>

          <div id="Images" class="tabpane">
            <div class="content-wrapper">

              <div class="nocontent-header">There are no images yet</div>
              <div class="nocontent-subhead">When something is uploaded, you will see it here.</div>

            </div>
          </div>

          <div id="About" class="tabpane">
            <div class="content-wrapper">

              <div class="about-center">
                <div class="about-titel">This is a titel</div>
                <div class="about-text">This is some text</div>
              </div>

            </div>
          </div>

        </div>
      </div>

How to implement this with a domain extension?

0

There are 0 best solutions below