admin管理员组文章数量:1415421
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
inputs[i].onfocus = foo;
}
function foo(){
alert(this.value);
}
When the input values are manually entered:
Above code works and alerts the correct values regardless of the type of an input field.
When the input values auto-filled by browser:
Code works and alerts the correct values when the input field is of type text. In case of a password field, it alerts empty string!
Is this behaviour because of the browser's security policies? Or there's any workaround possible? I tried it in Chrome browser.
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
inputs[i].onfocus = foo;
}
function foo(){
alert(this.value);
}
When the input values are manually entered:
Above code works and alerts the correct values regardless of the type of an input field.
When the input values auto-filled by browser:
Code works and alerts the correct values when the input field is of type text. In case of a password field, it alerts empty string!
Is this behaviour because of the browser's security policies? Or there's any workaround possible? I tried it in Chrome browser.
- Please clarify: are you having trouble accessing the "value" attribute that has been pre-populated by the page you have served, or are you talking about how Chrome pre-fills inputs with its password manager? If the latter, please see my answer. – CzechErface Commented Feb 19, 2016 at 8:07
- If the 'value' attribute is pre-populated, there's no issue. The problem is when browser auto-fills the input boxes with its password manager (when you say save password for this site). In this case, if browser does not insert a value attribute to the DOM element, then what does it essentially do? Thanks for your answer Czech, and it seems there's no way I solve this! – Akhil Dixit Commented Feb 19, 2016 at 8:31
2 Answers
Reset to default 2$(document).ready(function() {
$("input")
.blur(password)
.trigger('blur');
});
function password() {
alert($(this).val())
}
DEMO
This is an interesting question because I believe that you are referring to the side-effect of a security feature in Chrome.
Chrome (and probably other browsers) may not actually populate the DOM with the auto-filled password because a malicious programmer could write a JavaScript that attempts to steal auto-filled passwords from unsuspecting victims once a page loads.
You will most likely require that the user clicks a submit button before you can gain access to that information.
You might be able to force the form to submit and check the value right before the submit actually occurs. If that works, then you can just cancel the submission by returning false in "onsubmit" and you now have the password.
本文标签: jqueryIs there any way to get value of an autofilled password box in JavaScriptStack Overflow
版权声明:本文标题:jquery - Is there any way to get value of an auto-filled password box in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745210234a2647840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论