How to detect when browser places saved password?

守給你的承諾、 提交于 2019-12-11 01:36:35

问题


I'm trying to detect when a browser places a saved username and password into the input fields. I've looked at this post , but I don't have the option to change this functionality, and the other solutions on don't work.

Basically, I have a disabled login button if the fields are empty on load. But when a browser fills in the input, it doesn't enable the button. I'm trying to find how to see if it changes.

I'm using jQuery and JS.

I've tried .change, on .ready, on .load, and all. It seems to happen after the .load event.

Does anyone know a solution to this? I would like to avoid using any sort of timeout.


回答1:


I think there is no way to detect if the browser has some buil-in feature that pre-populates the fields.

You could solve the problem with the a timer that enable the button, if something is there. Something like this:

setInterval(function (){
 if($("#username").val()!=""){
  $("#loginbutton").attr("enabled","enabled"); 
 }
},1000)



回答2:


The key thing is that the field will be populated without there having being any keypresses in the field.

So if you trap .keypress on the input field to know if a key is any pressed, then if you get to submitting the form and find there were no keypresses despite a value being there - then you can be somewhat sure that the browser pre-populated it.

If you want to know before submitting (soon after the page loads), you'd want to run a check on an interval that sees if the value has changed despite no key presses.

As @japrescott pointed out, you might want to check for .focus as well in case the user pastes a value in.




回答3:


Haven't test this, but couldn't you simply compare the default values of each field to the values of each field after the page is loaded (or .2 seconds after the page is loaded if that's an issue)?




回答4:


Give a shoot to Jquery .live() function

$('.element').live('load', function(){
  enableLogin();
});


来源:https://stackoverflow.com/questions/13197539/how-to-detect-when-browser-places-saved-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!