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

203
Views
JS no captará un enlace si hay una imagen en la etiqueta de anclaje

Estoy usando un código que encontré para configurar los botones para compartir en redes sociales de Vanilla js. El código hace que los enlaces se abran en una ventana emergente. Funciona perfectamente bien si el contenido de la etiqueta es solo texto sin formato, pero si coloco una imagen dentro de la etiqueta, se abre la ventana emergente, pero no se le pasa el enlace. Es solo una ventana "acerca de: en blanco".

Aquí está el js:

 // create social networking pop-ups (function() { // link selector and pop-up window size var Config = { Link: "a.share", Width: 500, Height: 500 }; // add handler links var slink = document.querySelectorAll(Config.Link); for (var a = 0; a < slink.length; a++) { slink[a].onclick = PopupHandler; } // create popup function PopupHandler(e) { e = (e ? e : window.event); var t = (e.target ? e.target : e.srcElement); // popup position var px = Math.floor(((screen.availWidth || 1024) - Config.Width) / 2), py = Math.floor(((screen.availHeight || 700) - Config.Height) / 2); // open popup var popup = window.open(t.href, "social", "width="+Config.Width+",height="+Config.Height+ ",left="+px+",top="+py+ ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1"); if (popup) { popup.focus(); if (e.preventDefault) e.preventDefault(); e.returnValue = false; } return !!popup; } }());

Y aquí está el HTML. Si uso esto, funciona perfectamente:

 <a href="https://www.facebook.com/sharer/sharer.php?u=https://www.example.com/blog/test.html" class="facebook share">Facebook</a>

Pero si uso esto, se abre la ventana emergente, pero el enlace no se carga en la nueva ventana. Simplemente abre una ventana emergente "acerca de: en blanco":

 <a href="https://www.facebook.com/sharer/sharer.php?u=https://www.example.com/blog/test.html" class="facebook share"><img src="/static/images/facebook-share.png" width="64px"></a>

¿Cuál puede ser el problema?

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

0

La línea problemática es:

 var t = (e.target ? e.target : e.srcElement);

En el segundo caso, t se refiere a la etiqueta img y no tiene atributo href , por lo que la página se abre en negro.

Simplemente puede ejecutar una verificación en caso de que se devuelva a etiqueta img , busque la etiqueta principal.

 // create social networking pop-ups (function() { // link selector and pop-up window size var Config = { Link: "a.share", Width: 500, Height: 500 }; // add handler links var slink = document.querySelectorAll(Config.Link); for (var a = 0; a < slink.length; a++) { slink[a].onclick = PopupHandler; } // create popup function PopupHandler(e) { e = (e ? e : window.event); var t = (e.target ? e.target : e.srcElement); if (t.tagName === "IMG") { // look for parent "A" tag t = t.parentNode; while (t.tagName !== "A" && t.tagName !== "BODY") { t = t.parentNode; } } // popup position var px = Math.floor(((screen.availWidth || 1024) - Config.Width) / 2), py = Math.floor(((screen.availHeight || 700) - Config.Height) / 2); // open popup var popup = window.open(t.href, "social", "width="+Config.Width+",height="+Config.Height+ ",left="+px+",top="+py+ ",location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1"); if (popup) { popup.focus(); if (e.preventDefault) e.preventDefault(); e.returnValue = false; } return !!popup; } }());
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!