jquery - Is there any way to get value of an auto-filled password box in JavaScript? - Stack Overflow

var inputs = document.getElementsByTagName('input');for (var i = 0; i < inputs.length; i+

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.

Share Improve this question asked Feb 19, 2016 at 7:37 Akhil DixitAkhil Dixit 1351 gold badge2 silver badges8 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745210234a4616794.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信