How can I hide the password entered via a JavaScript dialog prompt?

Are you looking for the prompt function?

var response = prompt("What is your name?");

alert("Hello, " + response);

The dialog will look something like this:

enter image description here

This this probably isn’t the best way to get password input, because it does not mask the input. Instead, consider using an HTML form with a password input field.


Maybe you are looking for basic HTTP authentication instead?

You can set this by getting your web server to send a few headers; for example with PHP:

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>

This will cause the client to show a dialog like this:

enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)