By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

JavaScript Prompt will not appear...

P: 2
I just starte learning to code. I had a section of HTML, and tried to insert some JavaScript into it. It goes like this:
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. alert("Hello there");
  3. alert("I am the Matrix");
  4. var name  = prompt("Enter your name");
  5. alert("Hello" + name + ".");
  6. </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
Jul 18 '14 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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).

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>my page</title>
  5.   </head>
  6.   <body>
  7.     <p>Hello there, Iím the Matrix</p>
  8.     <label for="user-name">Enter your name</label>
  9.     <input type="text" id="user-name">
  10.     <!-- just skipping the real magic and using a simple button instead -->
  11.     <button type="click" id="read-name">thatís me</button>
  12.     <p id="greeting"></p>
  13.     <script type="text/javascript">
  14. // now for some JavaScript magic
  15. document.getElementById('read-name').addEventListener('click', function (evt) {
  16.   // get the name out of the box
  17.   var username = document.getElementById('user-name').value;
  18.   // put the name into the greeting area
  19.   document.getElementById('greeting').textContent = username;
  20. });
  21.     </script>
  22.   </body>
  23. </html>
  24.  
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)
Jul 18 '14 #2

P: 2
Ok thank you for your help
Jul 18 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.