<script>
var name = prompt("What is your name?");
if (name != null) {
alert("Hello " + name);
}
else {
alert("You did not tell me your name. ");
}
</script>
I cannot get this code to work (I'm a rank beginner). If I enter a name and click OK, it comes back with "Hello name" as it should. BUT... if I click CANCEL, it comes back with "Hello null" instead of "You did not tell me your name". Same thing if I don't enter a name and click OK.
I have tried this in the HEAD section. In the BODY section of my html page. No difference. I've tried it as a separate js file. I've played with spacing.
Cannot get it to work. This is from a book, and other code snippets they provide work, but not this one. Any help would be greatly appreciated.
var name = prompt("What is your name?");
if (name != null) {
alert("Hello " + name);
}
else {
alert("You did not tell me your name. ");
}
</script>
I cannot get this code to work (I'm a rank beginner). If I enter a name and click OK, it comes back with "Hello name" as it should. BUT... if I click CANCEL, it comes back with "Hello null" instead of "You did not tell me your name". Same thing if I don't enter a name and click OK.
I have tried this in the HEAD section. In the BODY section of my html page. No difference. I've tried it as a separate js file. I've played with spacing.
Cannot get it to work. This is from a book, and other code snippets they provide work, but not this one. Any help would be greatly appreciated.