skip to Main Content

Code## Question

The following HTML code includes a script to alert a message. However, the alert is not working when the page loads. The script is linked to an external JavaScript file. How can I fix this issue and make the alert work?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>replit</title>
  </head>
  <body>
    <div id="arjun">
      <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Vero aliquam deserunt minima, tempora iure amet rem accusamus, ea explicabo tempore saepe voluptas optio. Veritatis blanditiis magni molestia facilis distinctio! Ipsa!</p>
    </div>
    <!-- <script>alert('arjun is great')</script> -->
    <script src="script.js" type="text/javascript"></script>
  </body>
</html>
<!-- External Js code-->
alert('Arjun is Great');

Additional information

The script inside the HTML file works fine when uncommented. When linked to an external JavaScript file, the alert does not work.

2

Answers


  1. I believe your code is fine, but if the JS file doesn’t work, make sure to follow these steps:

    1.Check the file path

    2.Clear your browser cache, as it can cause issues

    3.Ensure that your JS file has a .js extension

    4.Make sure there are no typos in the code

    Login or Signup to reply.
  2. I tried your code and as @disinfor and @ray in the comment suggested the browser is not running your JavaScript file because the type attribute is spelled wrong

    You can either correct the spelling from text/javasscript to text/javascript (the same way it is in the code version you shared) or remove the type attribute altogether

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search