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

126
Visualizações
Prevent form submission but reflecting it in URL

Working with vanilla JS is there a "way" where i could essentially event.preventDefault while reflecting the URL on the form. I want to give people the choice to copy and paste the link and share to their friends. My javascript code is doing something like

searchKeywordForm.addEventListener('submit', async (event) => {
    event.preventDefault();

    //Clear all courseItems and start from scratch
    courseItems = [];
    courseContainerElement.innerHTML = "";
    let searchInput = document.getElementById("keywords");
    await fetchDataByKeywords(searchInput.value);
})

In so doing it's not reflected in the url that i can do file:///C:/Users/bobby/Desktop/twitdemy/index.html?keywords=cas I have already did some checks for queryString so essentially it works.

Right now since there is event.preventDefault the url is basically static at file:///C:/Users/bobby/Desktop/twitdemy/index.html

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use history.pushState() to modify the current url without reloading page

const searchKeywordForm = document.forms.keyform

searchKeywordForm.addEventListener('submit', (e) => {
  e.preventDefault();
  const searchInput = document.getElementById("keywords"),
        newUrl =  `${searchKeywordForm.action}?key=${searchInput.value}`;
        
  history.pushState(null, null, newUrl)
  // log current location to see changes
  console.log(location.href)
})
<form id="keyform" action="/formsubmiturl">
  <input id="keywords" value="foo" />
  <button>Submit</button>
</form>

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