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

446
Views
Evitar la recarga de la página en la etiqueta de anclaje html

¿Cómo evito que la etiqueta de anclaje vuelva a cargar mi página cuando hago clic en ella usando js?

 <a href='?code=$user_code' class=''>view user</a>
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como sugirió Barmar en los comentarios, puede agregar un detector de eventos al elemento ancla que llama a event.preventDefault() para evitar que se vuelva a cargar la página. Dentro del detector de eventos, puede obtener los datos de la base de datos.

 <a id="my-anchor" href='?code=$user_code'>view user</a>
 var myAnchor = document.getElementById('my-anchor');

myAnchor.onclick = function(e) {
 e.preventDefault();
 
 var href = myAnchor.getAttribute('href');

 // fetch data from database
};

https://jsfiddle.net/tojx5ske/

Otra opción es crear un ancla con href="#" y pasar el href real al detector de eventos usando atributos de datos :

 <a id="my-anchor" href="#" data-href='?code=$user_code'>view user</a>
 var myAnchor = document.getElementById('my-anchor');

myAnchor.onclick = function(e) {
 var href = myAnchor.dataset.href;

 // fetch data from database
};

https://jsfiddle.net/tojx5ske/1/

almost 4 years ago · Santiago Trujillo 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!