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 trialSloan Wolf
iOS Development Techdegree Student 1,330 Pointsissues 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
19 Answers
Unsubscribed User
3,801 PointsHave 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.
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointsyes. added. still nothing...
Unsubscribed User
3,801 PointsAs 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.
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointsthanks mark. hope i did this right-
Unsubscribed User
3,801 PointsI 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
iOS Development Techdegree Student 1,330 Pointsthat 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:
Unsubscribed User
3,801 PointsI 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
iOS Development Techdegree Student 1,330 Pointsthat'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...
Unsubscribed User
3,801 PointsIn 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");
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointsstarted a whole new one and should be exactly as you/ the lesson laid out. problem's still the same
Unsubscribed User
3,801 PointsIn this new snapshot, you have no code in your scripts.js file. You need to add:
alert("Hello from Treehouse");
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointsthat's the first line of my .js file
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointslooks 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
Unsubscribed User
3,801 PointsI 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
iOS Development Techdegree Student 1,330 Pointsso 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.
Unsubscribed User
3,801 PointsPerhaps 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
iOS Development Techdegree Student 1,330 Pointsstill 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
Unsubscribed User
3,801 PointsNo problem! Hope it can be resolved.
Sloan Wolf
iOS Development Techdegree Student 1,330 PointsUPDATE: 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?
Chyno Deluxe
16,936 PointsIt'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.
Sloan Wolf
iOS Development Techdegree Student 1,330 Pointstried doing that in both safari and chrome. still no pop up, still blank white box on the page with no text
Chyno Deluxe
16,936 PointsThat'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
222 PointsI have the same problem now both on my Laptop and Desktop. Hmmmm.
Rikard Röhr
222 PointsFor those who might have the same problem in the future. I restarted the class and made a new "Workspace". That solved it.
Tony Smith
Courses Plus Student 3,033 PointsFunny 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...
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 PointsCan you please post a snapshot of your workspace.