Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
¿Cómo pasar/agregar el parámetro de URL a todos los hrefs (enlaces) en una página?

Usando javascript, mi objetivo es consultar la URL y agregar parámetros a todos los enlaces en una página. Si el enlace ya contiene un parámetro, el enlace agregará los nuevos parámetros:

Página de acceso de usuario: https://example.com/?location=brazil
El enlace en la página era: https://link.com/?hello=ok
El enlace se convierte en: https://link.com/?hello=ok&location=brazil

Si no hay un parámetro anterior en el enlace, se convierte en:
https://link.com/?location=brasil

Probé el siguiente código:

 var queryString = new URL(window.location).search; document.querySelectorAll("[href]").forEach(link => { var current = link.href; link.href = current + queryString; }); </script>

Pero cuando un enlace ya contiene un parámetro, usa ? en lugar de & por lo que se convierte en:

https://link.com/?hello=ok?location=brasil

¿Cómo soluciono este problema?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

¿Podría comprobar si la variable queryString contiene un ? y reemplácelo con un & en su lugar.

Así que tu código sería algo así como

 var queryString = new URL(window.location).search; document.querySelectorAll("[href]").forEach(link => { var current = link.href; const queryStrToUse = queryString.replace('?', '&') link.href = current + queryStrToUse; });
about 4 years ago · Juan Pablo Isaza Report

0

No es una buena idea cambiar el atributo ancla href con javascript por muchas razones, pero si es necesario, puede hacerlo:

 document.querySelectorAll("a").forEach(link => { link.href = link.href.includes("?") ? link.href + queryString.replace("?", "&") : link.href + queryString; });
about 4 years ago · Juan Pablo Isaza Report

0

var queryString = new URL(window.location).search; document.querySelectorAll("[href]").forEach(link => { var current = link.href; link.href = `${current}${current.includes('?') ? queryString.replace('?', '&') : queryString}`; });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!