Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Introducing JavaScript Your First JavaScript Program

Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

issues with javascript alert

opened workspaces to write my first line in javascript. after i save/preview another window opens with a blank white box. no pop up alert, no text. tried on both safari and chrome as well as trying all suggestions under relevant thread. frustrated because i'm already stuck at step 1 and can't move forward

Can you please post a snapshot of your workspace.

19 Answers

Have you added the script tag into your index.html file?

<script src="/scripts.js"></script>

Without this, your HTML file will not execute the javascript.

As Chyno Deluxe requested previously, could you provide a snapshot of your workspace? There is a feature on Treehouse workspaces called 'snapshot', which allows you to share your workspace with others.

You can read how to create and share snapshots here.

I spotted a few errors in your code, and will try to explain what is wrong and why.

Here is your HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
  <title>JavaScript Basics</title>
 </head>
<body>
  <div class="container">
  <script src='scripts.js'></script>
  </div>
</body>
</html>
<script src="/scripts.js"></script>
<script>
alert("Hello From Treehouse");
</script>

The problem here is you have script tags outside of the 'head' and 'body' tags (and even outside of the 'html' tag). In order for script tags to be valid, they must be inside either the 'head' tag, or the 'body' tag. Also, you only need to reference your scripts.js file once in your HTML files.

It should be:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
  <title>JavaScript Basics</title>
 </head>
<body>
  <div class="container">
  <script src='scripts.js'></script>
  </div>
</body>
</html>

Here is your JavaScript file:

<script>
alert(Hello from Treehouse);
</script>
document.write("<h1>Welcome to JavaScript Basics</h1>");

There is one really big problem here; you have used HTML tags inside a .js file. This is invalid syntax. Anything inside the .js file, should be JavaScript syntax only.

EDIT: The code was also missing double quotation marks inside the alert.

Your code should be:

alert("Hello from Treehouse");
document.write("<h1>Welcome to JavaScript Basics</h1>");

I hope this helps.

Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

that was the "default" code. html was already in and .js file the same as above, just as taught in video. the additional code/ invalid syntax was an attempt to put some kind of fix in. so, back to my starting point. code is correct, still no pop up, just a new window with blank white box.

new snapshot so you can see:

https://w.trhou.se/9rtrexwxse

I am sorry, I overlooked that the alert was missing double quotations around the text. It should be:

alert("Hello from Treehouse");
document.write("<h1>Welcome to JavaScript Basics</h1>");
Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

that's what i already have? read through both twice, unless i'm missing something obvious (definitely possible) - they look the same. in my .js file and identical to the code you posted above...

In the last snapshot you posted, the alert was missing the quotation marks needed to make it valid.

Your alert:

alert(Hello from Treehouse);

Correct syntax:

alert("Hello from Treehouse");

In this new snapshot, you have no code in your scripts.js file. You need to add:

alert("Hello from Treehouse");
Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

looks like .js is blank when i click the snapshot link, but i can post screenshot. the exact code is there. man, all sorts of technical issues straight out the gate

I created a new workspace and took a snapshot of it. The code uses an alert and a document.write, and works correctly on my computer. You can view it here.

You can press 'Fork Snapshot' in the top right, and it will let you preview the code inside your own workspace area. Try it out in Chrome just to make sure (I cannot test with Safari). If you don't see the alert, then there must be a problem unrelated to the code syntax.

Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

so odd! when i forked your snapshot and previewed, i got the pop up! copied and pasted your exact code to my workspace and nothing?!

baffled.

Perhaps the preview of your own workspace is caching the old files (which contain errors)?

In Chrome, click 'preview' again in your workspace, and on the new page that opens, press Shift + F5. This will refresh the page as well as the cache.

Does it work now? If not, I think the problem has gone beyond my help, and you might want to reach out Treehouse staff.

Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

still nothing. 2:22am, so i'm gonna tap out for the night. sent a support request to treehouse, so we'll see what they say. thanks for all your help mark! really appreciate it

No problem! Hope it can be resolved.

Sloan Wolf
seal-mask
.a{fill-rule:evenodd;}techdegree
Sloan Wolf
iOS Development Techdegree Student 1,330 Points

UPDATE: never received an answer from treehouse, BUT

did this same exercise on my desktop and it worked perfectly. must be some issue with settings on my laptop?

It's possible maybe you clicked the option that prevents the page from opening additional dialog boxes. Usually, closing the tab, opening the web page on a new tab fixes this issue. Could be the reason why the alert doesn't display on your laptop but does on your desktop.

I hope this helps.

That's strange. How about creating a new scripts page and entering code in there and directing your html script tag to the new .js?

Rikard Röhr
Rikard Röhr
222 Points

I have the same problem now both on my Laptop and Desktop. Hmmmm.

Rikard Röhr
Rikard Röhr
222 Points

For those who might have the same problem in the future. I restarted the class and made a new "Workspace". That solved it.

Tony Smith
PLUS
Tony Smith
Courses Plus Student 3,033 Points

Funny thing, I just had the same problem. Had to fork it in order for it to work. I would recommend making sure this is fixed, we pay good money for these courses and they should work. There have been several code chellenges so far that won't except the correct answer because its looking for a more basic answer. The code should take any correct answer...