非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。

PasswordCredential 构造函数创建新 PasswordCredential 对象。 In supporting browsers, an instance of this class may be passed the credential init object for global fetch .

句法

var myCredential = new PasswordCredential(passwordCredentialData)
var myCredential = new PasswordCredential(HTMLFormElement)
					

参数

Either of the following:

passwordCredentialData
A PasswordCredentialData dictionary containing the following fields:
  • iconURL : (Optional) the URL of a user's avatar image.
  • id : The ID of the user signing in.
  • 名称 : (Optional) The name of the user signing in.
  • password : The password of the user signing in.
htmlFormElement
A reference to an HTMLFormElement with appropriate input fields. The form should, at the very least, contain an id and password. It could also require a CSRF token.

范例

This example shows how to set up an HTMLFormElement to caputure data which we'll use to create a PasswordCredential 对象。

Starting with the form element.

<form id="form" method="post">
  <input type="text" name="id" autocomplete="username" />
  <input type="password" name="password" autocomplete="current-password" />
  <input type="hidden" name="csrf_token" value="*****" />
</form>
					

Then, a reference to this form element, using it to create a PasswordCredential object, and storing it in the browser's password system.

var form = document.querySelector('#form');
var creds = new PasswordCredential(form);
// Store the credentials.
navigator.credentials.store(creds)
  .then(function(creds) {
  // Do something with the credentials if you need to.
});
					

规范

规范 状态 注释
Credential Management Level 1 工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
PasswordCredential() 构造函数 非标 Chrome 51 Edge ≤79 Firefox ? IE ? Opera ? Safari ? WebView Android 51 Chrome Android 51 Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android 5.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

元数据

  • 最后修改: