Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

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

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");

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...