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

298
Views
¿Cómo puedo ejecutar código en el evento onload en un archivo y en una etiqueta de script?

Deseo agregar código a un evento de clic en un archivo y una etiqueta de secuencia de comandos. Pero parecen estar en conflicto. ¿Cómo puedo conseguir esto?

JavaScript:

 window.onload = function() { document.getElementById("button3").addEventListener("click", respond3); } function respond3(e) { alert("Way to go!!"); }

html:

 <head> <title>Second Javascript</title> <script src="Second.js"></script> <script> window.onload = function() { document.getElementById("button2").addEventListener("click", respond); } function respond(e) { alert("getting better"); } </script> </head> <body> <h1>The quick brown fox jumps over the lazy dogs</h1> <button onclick='alert("bad practice");'>Inline</button> <button id='button2'>Script tag</button> <button id='button3'>Separate file</button> </body>

Por sugerencia de Quentin, cambié la etiqueta del script a esto:

 <script> window.addEventListener('load', AddClick2) function AddClick2() { document.getElementById("button2").addEventListener("click", respond); } function respond(e) { alert("getting better"); } </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Las propiedades on* solo pueden tener asignada una función. No es tanto un conflicto, ya que simplemente está sobrescribiendo la primera función de onload con la segunda.

Si bien podría hacer algo como verificar si ya hay una función allí, luego copiarla en una nueva variable, luego llamarla desde la nueva variable dentro de su nueva función de carga … eso se complica.

Utilice addEventListener en su lugar.

 window.addEventListener('load', a_function); window.addEventListener('load', a_different_function);
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!