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

Benjamin Doggett
Benjamin Doggett
486 Points

I am currently a little confused on the concepts of adding <scripts> to JavaScript. Also, what are they overall?

I'm new to programming and have a real desire to learn all I can. I'm currently working with Javascript and have been learning about <scripts>. I have been stuck on the "homework" problem stating that I need to, "add opening and closing script to the body." If I could get some assistance and how to do this and what <scripts> are in more depth that would be great. Thank you for your time.

3 Answers

Collin Halliday
seal-mask
.a{fill-rule:evenodd;}techdegree
Collin Halliday
Full Stack JavaScript Techdegree Student 17,491 Points

Hey, Benjamin.

You need to add opening and closing <script> tags in order to use JavaScript in your HTML document. I believe the best practice is to include your <script> tags right before the closing </body> tag. This way, the HTML and CSS have a chance to load before the JavaScript runs.

Between the <script> tags, you can include either JavaScript code, or a link to an external JavaScript file through the source attribute. The following code snippet provides an example of linking to the external JavaScript file:

<body> //opening body tag

    <h1>Some WebPage</h1>

<script src="app.js"></script> </body> //closing body tag

In this example, you add the URL of the external script file in quotes after the "src" attribute and an "=" sign. You could place this same link between the HTML document's <head> tags, but you should probably avoid that practice. You can also write your JavaScript code directly inside of your HTML document between the <script> tags. The following code snippet provides an example of this:

<body>

<h1>Some WebPage</h1>

<script> alert("Warning!"); </script> </body>

When run, this code should result in an alert box displaying on your webpage with the text "Warning!". This same functionality can be accomplished by moving the code between the <script> tags in this example to a separate document. You could call this document "app.js", or whatever you want really, as long as you include the ".js" extension at the end of the filename. You can then access the code in that file by including the URL in the source attribute on the opening <script> tag, as demonstrated in the example above.

I hope that helps. Best of luck!

Benjamin Doggett
Benjamin Doggett
486 Points

Thanks so much for taking the time to help me out. I really appreciate it, and that was a very helpful walkthrough. I was able to write down some great notes for future studying. Thank you so much!

Agnes Demes
Agnes Demes
6,613 Points

good luck and happy coding :)

Agnes Demes
Agnes Demes
6,613 Points

Hi, so the challenge is asking you to link the java script file to the html page. This is done by adding the script html tags to thehtml file. between the body tag of the html add the opening and closing script tags. <script></script>

Agnes Demes
Agnes Demes
6,613 Points

also if you find that these challenges are too difficult , it might be a good idea to start with the basic HTML course which will give you some good foundations.

https://teamtreehouse.com/library/html-basics-2

Benjamin Doggett
Benjamin Doggett
486 Points

Thank you for your help! That sounds like a great idea to learn HTML. I'll go ahead and do that so I can jump back into JavaScript with a firmer foundation. I really appreciate the advice given!