2

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>
Eric Klaus
  • 807
  • 1
  • 7
  • 20

4 Answers4

7

There is a syntax error in document.write statement.

Write it as follow

document.write("welcome to my page "+ user_name + "!");
Divyesh Savaliya
  • 2,453
  • 2
  • 13
  • 31
0

There is one too many ")"

document.write("welcome to my page " + user_name + "!");
jbrond
  • 696
  • 6
  • 18
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>
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • it more a problem with `document.write` in the head. and a [possible problem](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) with `document.write` at all. – Nina Scholz Apr 20 '16 at 08:58
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.

Richard Vanbergen
  • 1,524
  • 12
  • 24