如何隐藏通过 JavaScript 对话框提示输入的密码?

2022-01-22 00:00:00 passwords javascript

如何在 JavaScript 的对话框提示中隐藏用户输入的密码?例如,使用类似的东西

How can I hide the password entered by a user in a dialog prompt in JavaScript? For example, using something like

var passwd = prompt("Enter Password : ", "your password here");

我希望在例如输入12345,在对话框中显示为*****......

I would like that when e.g. 12345 is entered, it appears like ***** or ..... in the dialog box.

谁能建议我如何做到这一点或提供一些示例代码?

Can anyone suggest how I can do this or provide some example code?

推荐答案

你在寻找 提示函数?

Are you looking for the prompt function?

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

alert("Hello, " + response);

对话框将如下所示:

这可能不是获取密码输入的最佳方式,因为它不会屏蔽输入.相反,请考虑使用带有密码输入字段的 HTML 表单.

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.

也许您正在寻找基本的 HTTP 身份验证?

Maybe you are looking for basic HTTP authentication instead?

您可以通过让您的网络服务器发送一些标头来进行设置;以 PHP 为例:

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:

相关文章