JavaScript Prompt will not appear...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pmadams3
    New Member
    • Jul 2014
    • 2

    JavaScript Prompt will not appear...

    I just starte learning to code. I had a section of HTML, and tried to insert some JavaScript into it. It goes like this:
    Code:
    <script>
    alert("Hello there");
    alert("I am the Matrix");
    var name  = prompt("Enter your name");
    alert("Hello" + name + ".");
    </script>
    Whenever I run it, the prompt does not work. This is what appears:
    Hello there.
    I am the Matrix.
    Helloundefined.

    Other notes:
    I am using notepad on Windows 8 to type the code.
    Whenever I open the page, there is a pop up that asks me if I want to allow blocked content. I have to click this to allow the pop ups to run.
    The file is saved as an HTML file, and around that script I have HTML.

    If you can please help. Thank you
    Last edited by Dormilich; Jul 18 '14, 05:38 PM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Whenever I open the page, there is a pop up that asks me if I want to allow blocked content. I have to click this to allow the pop ups to run.
    that would be the source of the problem. try it with Firefox, Chrome or Safari.

    however, in today’s applications there is hardly a need of using prompt() and alert().

    when you request data from a user, you would normally have an input field where (s)he can type in the value and then a button to start the processing action (when a click on it was encountered).

    Code:
    <!DOCTYPE html>
    <html>
      <head>
        <title>my page</title>
      </head>
      <body>
        <p>Hello there, I’m the Matrix</p>
        <label for="user-name">Enter your name</label>
        <input type="text" id="user-name">
        <!-- just skipping the real magic and using a simple button instead -->
        <button type="click" id="read-name">that’s me</button>
        <p id="greeting"></p>
        <script type="text/javascript">
    // now for some JavaScript magic
    document.getElementById('read-name').addEventListener('click', function (evt) {
      // get the name out of the box
      var username = document.getElementById('user-name').value;
      // put the name into the greeting area
      document.getElementById('greeting').textContent = username;
    });
        </script>
      </body>
    </html>
    two more advices though

    - get a decent code editor with code highlighting, e.g. Notepad++

    - regularly check the browser’s error console (should pop up when you hit F12 on most browsers)

    Comment

    • pmadams3
      New Member
      • Jul 2014
      • 2

      #3
      Ok thank you for your help

      Comment

      Working...