Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Introducing JavaScript Write Another Program

how do I call an alert(); to a javascript tag

A task has asked me to write a program inside a script tag that would open a alert(); that said warning!. I attempted the code and the box with the message appeared now it says my script tag no longer works.

index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
  <script src="scripts.js"></script>
  <script>
    alert("Warning!");
  </script>
</body>
</html>

Remove the <script src='scripts.js'></script> It is probably getting an error due to file location since the script runs lexicographically. If there is an error the code will not proceed.

thanks for responding, the task ask for the script tag first (which it said I did correct) then it wants the alert();. It says the problem is that I didnt call the alert to the script tag. I'm wondering if I didnt manage to put the alert inside the script tag?

Thanks everyone for youre help! I finally got it. I was using the wrong syntax. I thought <script src="scripts.js"></script> was the script tag when in fact the script tag is <script> </script>. The issue was that the challenge was telling me I did task one right when I didnt.

4 Answers

You don't need all that code.

<script> alert('Warning'); </script> 

placed inside the body tags will do it.


This isn't necessary: <script src='script.js'> </script>

src= is specifying an external file. JavaScript is a SCRIPTING Language. Which means it executes from the first to the last line provided there aren't errors. Here you are telling the HTML doc to look for script.js located in the current directory and execute the code located within. The problem is script.js doesn't exist.

I'm not positive, but I think that the disconnect is from understanding that <script src='script.js'></script> isn't a configuration file, like bootstrap has a framework needed to reference in order to work. The browser has built in JavaScript functionality so anything inside those script tags will be interpreted appropriately.

Thanks, yeah i figuerd that out earlier this morning. I thought <script src=""></script> was what they meant by script tag but I realized it was way more basic just <script> </script>. The problem was when I was doing the code wrong in the first take it told me I was right but that set me up for failer in the second task

Angela Visnesky
Angela Visnesky
20,927 Points

Hi William! Gerald was correct that you needed to remove that line of code. You also need to write a function around the alert, then call your function.

function myFunction() {
    alert("Hello! I am an alert box!");
}
myFunction();

The above snippet is close to what you need to pass the challenge. Make sure you replace the text for the alert. Hope this helps!

Thank you for responding, what would go inside the () behind myFunction

Instantiating isn't necessary since they do not want a function. They simply want you to call alert(); inside script tags.

Angela Visnesky
Angela Visnesky
20,927 Points

The brackets stay empty because you aren't passing any information into the function. A helpful source to practice with javascript is https://jsfiddle.net/. You can write functions and run them to see what happens.

I am a little confused about why I take out <scripts src="scripts.js"></script> when the task say to place the alert(); inside the script tag.

Angela Visnesky
Angela Visnesky
20,927 Points

<script src="scripts.js"></script> is calling a file that doesn't exist. It's extraneous code. You do need to wrap your function in the script tags for them to work on the page.

<script>
     function myFunction() {
         alert("Hello! I am an alert box!");
    }
    myFunction();
</script>