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 prompt executing before document.write statements ?

I am trying to write the code for the ultimate quiz challenge but the code I have written will not execute as I expect.

without the prompt statement the document.writes appear in the document line line as I expect.

But when I put the prompt command in it ignores them until the end and then writes them to the html page ignoring all the html written to the page before hand now matter where i load the script file or code directly into the page.

MY code:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/main.css"> <title>The Ultimate Quiz Challenge</title> </head> <body> <div class="container"> <h1>The Ultimate Quiz Challenge</h1> <script> document.write("<h3> " + "Welcome to the ultimate quizz challenge" +"</h3>"); document.write("<p> "+"Hi I will ask you five questions and then rank you" + "</p>"); var question1 ="<p>What is the capital of England</p>"; var firstanswer ="London"; var question2 = "<p>How many sides are there to a square</p>"; var secondanswer = 4; var noofquestions = 2; var count = 1 /var temp = eval('question' +1); */ /*document.write(temp);/ /* main loop asking questions */ while (count <= 2) { var temp = eval('question' + count); document.write(temp); var answer = prompt("Please type your answer "); count++; } </script> </div> </body> </html>

4 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: Browsers vary in how they handle document writes during initial page load.

Previously, it was common for browsers to display the page while it was being constructed. But most modern browsers do not display the page until all loading (including the running of any JavaScript) is completed.

So now, in most browsers, you will see any alert or prompt statements as they execute, but you will only see the results of document.write after the entire script is finished.

So is it best to only execute scripts after the page has loaded.. Using an Onload command or event

Micah Murray
Micah Murray
6,913 Points

If you are building code that interacts with the user its best to execute it after the page loads.

Micah Murray
Micah Murray
6,913 Points

This is what you should do. Put the code that ask the questions in a function and allow the user to call the function by clicking a button after the page loads. This way your initial statements are displayed before the loop begins. Try using a do while loop to ask the questions, it will execute the code block at least once before checking the test condition.

Ok wow thanks to everyone I learnt lots and we'll how little I know. It does now seem an odd language. I am used to a top down exercution as in first line typed first line executed like C or . Still the more I play the more I will hopefully learn. Cheers