Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
How to make custom URLs for different website states?

In my project I have a page where there are different directories inside other directories, which show and hide with "style.display="block/none", problem is that I cannot figure out how to save those states in an URL so they can be sent to others.

Ideally after user clicks on the button to "Show Section 1", and then on "Show section 11" the URL becomes website.com/section1/section11 that he can share to others.

So question is, how do I save states of websites to be shared based on where in the directory the user is?

Example code:

JS

const Section1 = document.getElementById("section1");
const Section2 = document.getElementById("section2");
const Section11 = document.getElementById("section1-1");
const Section12 = document.getElementById("section1-2");

function show1(){
     Section1.style.display="block";
     Section2.style.display="none";
}

function show2(){
     Section1.style.display="none";
     Section2.style.display="block";
}


function show11(){
     Section11.style.display="block";
     Section12.style.display="none";

}

function show12(){
     Section11.style.display="none";
     Section12.style.display="block";

}

And so on...

HTML


<button onclick="show1()">Show Section 1</button>
<button onclick="show2()">Show Section 2</button>

<div id="section1" style="display: block">

     <button onclick="show11()">Show Section 11</button>
     <button onclick="show12()">Show Section 12</button>

     <div id="section1-1" style="display: block">

          <p>Some text</p>
          <button onclick="show111()">Show Section 111</button>
          <button onclick="show112()">Show Section 112</button>

     </div>

     <div id="section1-2" style="display: none">

          <p>Some text</p>
          <button onclick="show121()">Show Section 121</button>
          <button onclick="show122()">Show Section 122</button>

     </div>

</div>

<div id="section2" style="display: none">

<p>Something in here...</p>

</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try something like this:

var url = ""

function show(target) {
  $(target).siblings("[id^=section]").hide();
  $(target).show();

  generateUrl();
}

function generateUrl() {
  url = "/";
  url += $("[id^=section]:visible").map(function() {
    return $(this).attr("id")
  }).get().join("/");
  console.log(url)
}

Since you tagged jquery then I thought I would provide you an example with less code. I will work properly once your html is complete.

But it general it generates the url based on the div's where the id start with section and it's visible.

Demo

var url = ""

function show(target) {
  $(target).siblings("[id^=section]").hide();
  $(target).show();

  generateUrl();
}

function generateUrl() {
  url = "/";
  url += $("[id^=section]:visible").map(function() {
    return $(this).attr("id")
  }).get().join("/");
  console.log(url)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="show('#section1')">Show Section 1</button>
<button onclick="show('#section2')">Show Section 2</button>

<div id="section1" style="display: block">

  <button onclick="show('#section1-1')">Show Section 11</button>
  <button onclick="show('#section1-2')">Show Section 12</button>

  <div id="section1-1" style="display: block">

    <p>Some text</p>
    <button onclick="show('#section1-1-1')">Show Section 111</button>
    <button onclick="show('#section1-1-2')">Show Section 112</button>

  </div>

  <div id="section1-2" style="display: none">

    <p>Some text</p>
    <button onclick="show('#section1-2-1')">Show Section 121</button>
    <button onclick="show('#section1-2-2')">Show Section 122</button>

  </div>

</div>

<div id="section2" style="display: none">

  <p>Something in here...</p>

</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda