1

I'm following tutorial cideon on JavaScript and wrote some examples in Notepad++ and saved them as something.html. The problem is when I open it with IE or Chrome, the code between <script> and </script> tags doesn't run at all. What is wrong with that?

<!doctype html>
<html>
<head>
 <title>Example of prompt()</title>
 <script>
 var user_name;
 user_name = prompt("What is your name?");
 document.write("welcome to my page ")
 + user_name + "!");
 </script>
</head>
</html>
asked Apr 20, 2016 at 8:42
7
  • 2
    document.write("welcome to my page " + user_name + "!"); try this Commented Apr 20, 2016 at 8:43
  • 1
    Look at the console. You will see an error there... Commented Apr 20, 2016 at 8:44
  • you have a syntax error (closing a parentheses too early) Commented Apr 20, 2016 at 8:44
  • Yeah, Sorry/ I missed that paranthesis. Commented Apr 20, 2016 at 8:47
  • @LPK, where I can find errors on console? Commented Apr 20, 2016 at 8:47

4 Answers 4

7

There is a syntax error in document.write statement.

Write it as follow

document.write("welcome to my page "+ user_name + "!");
answered Apr 20, 2016 at 8:44
Sign up to request clarification or add additional context in comments.

Comments

0

There is one too many ")"

document.write("welcome to my page " + user_name + "!");
answered Apr 20, 2016 at 8:46

Comments

0

When you open in chrome. Press F12 or Right Click> Inspect Element. Make sure you select "Console" at the top. Pay attention and try to understand what the console tells you.

In your case, this is what it says when I paste that code into my console:

enter image description here

"Unexpected token" This means that it can't understand the script, it was expecting a ; or a new line because you have a bracket at the end. Remove that and it should work.

answered Apr 20, 2016 at 8:54

Comments

0

First, move the script part out of the head to the body tag.

Second, write

document.write("welcome to my page " + user_name + "!");

in one line and remove the first closing parenthesis.

<!doctype html>
<html>
 <head>
 <title>Example of prompt()</title>
 </head>
 <body>
 <script>
 var user_name;
 user_name = prompt("What is your name?");
 document.write("welcome to my page " + user_name + "!");
 </script>
 </body>
</html>

answered Apr 20, 2016 at 8:47

1 Comment

it more a problem with document.write in the head. and a possible problem with document.write at all.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.