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

158
Visualizações
Appending current URL parameters onto anchor link

At a page like

https://www.example.com/?firstname=Steven&lastname=Smith&email=steve%40gmail.com&phone=0404555555

I have a button (anchor link) #ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell that links to https://www.example.com/form

I'd like to append the URL parameters from the current URL to the button's URL, so that the button's href is now https://www.example.com/form/?firstname=Steven&lastname=Doig&email=steve%40gmail.com&phone=0404555555

How can I do this with JavaScript please?

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

0

Use window.location.search:

var url = "https://exmaple.com";
var newurl = url + window.location.search;

newurl will contain all the get (ex. ?something=something&something2=something5) data.

To change the href of a:

var button = document.getElementById('#ptsBlock_553944');
button.href = button.href + window.location.search;
about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't care about supporting older browsers you can use the URL API and URLSearchParams.

function appendCurrentUrlSearchParams(anchorElement) {
  const currUrlSearchParams = new URL(window.location.href).searchParams;
  const link = new URL(anchorElement.href);

  // uncomment this line if you want to clear query parameters already present in the anchor url
  // link.search = '';

  for (const entry of currUrlSearchParams.entries()) {
    link.searchParams.append(entry[ 0 ], entry[ 1 ]);
  }
  
  anchorElement.href = link.href;
}

Usage in your case:

appendCurrentUrlSearchParams(document.querySelector('#ptsBlock_553944 .ptsCell:nth-of-type(1) .ptsEditArea.ptsInputShell'));
about 4 years ago · Juan Pablo Isaza Relatório

0

Read Html select using select to change the link of a button with Javascript

specifically the section on

  1. Get the element with something like document.getElement MDN getElement Link

  2. Change the .href of that element to what you want.

function selectFunction() {
    var x = document.getElementById("selectopt").value;

    document.getElementById("mylink").innerHTML = x;
  
    document.getElementById("mylink").href = "http://www." + x + ".com";
  
}
document.location.pathname = '/questions/69240453/appending- 
current-url-parameters-onto-anchor-link/69240510';

//get the document pathname I chose from document.location
let data = document.location.pathname;

let preUrlString = 'www.example.com/form';

let newString = preUrlString + data;

console.log(newString);
'www.example.com/form/questions/69240453/appending- 
current-url-parameters-onto-anchor-link/69240510'

document.getElementById("mylink").href = newString;
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