如何隐藏通过JavaScript对话框提示输入的密码? [英] How can I hide the password entered via a JavaScript dialog prompt?

查看:179
本文介绍了如何隐藏通过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?

您可以通过让您的Web服务器发送一些标题来设置它;例如使用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:

这篇关于如何隐藏通过JavaScript对话框提示输入的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆