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

65
Views
El aviso de Javascript se carga antes de que se muestre la página

Agregué un aviso en mi página, pero se carga antes de que se cargue la página. ¿Cómo solo muestro el mensaje una vez que se ve toda la página?

Aquí está mi aviso:

 if (name == null || name == "") { txt == "No name provided"; } else { txt = "Hello, " + name + "! How are you today?"; } alert(txt);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Si envuelve el código en un detector de eventos que escucha el evento DOMContentLoaded , se ejecutará solo una vez que el documento esté listo:

 window.addEventListener('DOMContentLoaded', (e)=>{ if (name == null || name == "") { txt == "No name provided"; } else { txt = "Hello, " + name + "! How are you today?"; } alert(txt); });

Sin embargo, he ajustado su código original a:

 window.addEventListener('DOMContentLoaded', (e) => { // assign the name via the prompt() interface, and // declare both variables (rather than accidentally // creating globals): let name = prompt("Hi, what's your name?"), txt; // name won't be null, but it may be falsey, so here // we check if the name is falsey: if (!name) { // if the user cancels the prompt, prompt() returns // false; if they provide an empty-string that // empty string is falsey: txt = "No name provided"; // if the name variable is truthy: } else { // we use a template literal String to interpolate // the variable 'name' into that string: txt = `Hello, ${name}! How are you today?`; } // I wouldn't recommend alert, but I left this unchanged: alert(txt); });

Demostración de violín JS .

Referencias:

  • alert() .
  • Evento DOMContentLoaded .
  • EventTarget.addEventListener() .
  • prompt() .
  • Template literals .
  • window
about 4 years ago · Juan Pablo Isaza Report

0

Entonces, para resolver el problema de las cargas rápidas antes de que se muestre la página:

  • coloque su etiqueta de secuencia de script justo antes del cierre </body> no en la etiqueta <head> .
  • Otra opción, como sugiere David Thomas , es escuchar el DOM que se ha cargado con DOMContentLoaded & addEventListener o simplemente con window.onload .
about 4 years ago · Juan Pablo Isaza Report

0

Una de las formas más sencillas de hacer esto debe ser: window.onload = alert("text"); . Además, considere colocar el código justo antes de la etiqueta del body de cierre o usar un detector de eventos de algún tipo para un mayor control sobre la visualización del mensaje.

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!