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

173
Views
JavaScript (Alert Message)

I have written a JavaScript alert message code it displays the alert message first in both codes 1 and 2 before Html content. What should I do to run the HTML content first and then a javascript code?

Code 1

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World !</title>

    </head>
    <body>
        <h1>I am learning JavaScript.</h1>
        <div id="Content">
            <p>This is a paragraph tag. Here the content of html.</p>
        </div>
        <script src="text.js"></script>
    </body>
</html>

Code 2

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World !</title>
    </head>
    <body>
        <h1>I am learning JavaScript.</h1>
        <div id="Content">
            <p>This is a paragraph tag. Here the content of html.</p>
        </div>
        <script>
alert(" This is alert message.");
</script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

There is a load event, which will fire AFTER the page is loaded. You can listen to the event:

window.addEventListener('load', function() {
    console.log('All assets are loaded');
    alert ("This is an alert MSG");
})
about 4 years ago · Juan Pablo Isaza Report

0

You can use setTimeout() for this

<html>
    <head>
        <title>Hello, World !</title>
        </head>
        <body>

          <h1>I am learning JavaScript.</h1>

          <div id="Content">
             <p>This is a paragraph tag. Here the content of html.</p>
          </div>

        <script>
         setTimeout(()=>{
           alert(" This is alert message.")     
          },2000)

        </script>
      
      </body>
  
   </html>
about 4 years ago · Juan Pablo Isaza Report

0

In your first code just put defer attribute as below

<script defer src="text.js"></script>

A script that will be downloaded in parallel to parsing the page, and executed after the page has finished parsing

for more detail refer to the URL

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

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!