解決瀏覽器會自動填充密碼的問題
解決辦法是在form上或input上添加autoComplete="off"這個屬性。
form表單的屬性如下所示:
但是這個解決方案在谷歌和火狐上均有bug,下面來一個一個解決。
1.'autocomplete="off"'在Chrome中不起作用解決方案
網(wǎng)站項目中,有登錄和注冊的彈框,在除chrome的瀏覽器中一切都ok,一旦在谷歌瀏覽器中,問題來了:
首先從登錄彈框中登陸成功,chrome會彈出是否保存密碼的提示框,點擊保存密碼按鈕,
然后接著退出賬戶,
這時打開注冊彈框,你會發(fā)現(xiàn)注冊彈框中用戶名和密碼也被默認填寫進去了(登錄彈框中默認填寫進去符合邏輯),
這現(xiàn)象就詭異了,開始各種查,cookie,本地緩存,等等,都解決不了這問題;
查閱后,很多沒有這個的解決方案。
1 通常我們會在form表單上加入autocomplete="off" 或者 在輸入框中加入autocomplete="off"
<form method="post" action="" name="login" autocomplete="off"> </form> //或者 <input id="name" type="text" name="name" maxlength="20" autocomplete="off">
2 但是有一種情況例外,就是表單中有input[type="password"],點擊保存密碼后,在Chrome瀏覽器則自動填充了用戶名和密碼的輸入框;為了統(tǒng)一樣式,我們需要就對Chrome的問題經(jīng)行單獨處理。
總結(jié)了4種解決方案,如下:
1 修改disabled屬性
if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1){ var inputers = document.getElementsByTagName("input"); for(var i=0;i<inputers.length;i++){ if((inputers[i].type !== "submit") && (inputers[i].type !== "password")){ inputers[i].disabled= true; } } setTimeout(function(){ for(var i=0;i<inputers.length;i++){ if(inputers[i].type !== "submit"){ inputers[i].disabled= false; } } },100) }
2 去除輸入框的name和id屬性
if(navigator.userAgent.toLowerCase().indexOf("chrome") != -1){ var inputers = document.getElementsByTagName("input"); for(var i=0;i<inputers.length;i++){ if((inputers[i].type !== "submit") && (inputers[i].type !== "password")){ var input = inputers[i]; var inputName = inputers[i].name; var inputid = inputers[i].id; inputers[i].removeAttribute("name"); inputers[i].removeAttribute("id"); setTimeout(function(){ input.setAttribute("name",inputName); input.setAttribute("id",inputid); },1) } } }
3.可以在不需要默認填寫的input框中設(shè)置 autocomplete="new-password"
網(wǎng)上咱沒有找到對其詳細解釋,但是發(fā)現(xiàn)163郵箱的登錄注冊是這么用的,
所以就借鑒借鑒咯,測試之后也是可以解決問題的,也是最簡單的解決辦法,網(wǎng)易給您點個贊!
4 修改readonly屬性
<input type="password" readonly onfocus="this.removeAttribute('readonly');"/>
但Firefox中有個Bug。首次提交后,F(xiàn)F會提示是否記住某某網(wǎng)站的密碼,點擊“記住”后 input[type=text]設(shè)置autocomplete="off"將不起作用。
有兩種情況:
1,form中沒有input[type=password],autocomplete="off"將起作用
2,去掉form,設(shè)置input[type=text]的autocomplete也起作用(測試不好用)
3.Firefox則需要使用另一個擴展屬性disableautocomplete (測試也不行)
<input type="text" disableautocomplete autocomplete="off" id="number"/>
火狐現(xiàn)在也沒有解決的辦法,,誰有麻煩告知一下哈。。。。。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
新增加的內(nèi)容是如何將div的scrollbar自動移動最下面
在做動態(tài)增長的div時,一般都是將內(nèi)容append到div的最下面,但這會帶來一個問題,那就是新增加的內(nèi)容會被遮在最下面,具體實現(xiàn)如下,感興趣的朋友可以參考下2014-01-01Javascript中判斷一個值是否為undefined的方法詳解
這篇文章給大家詳細介紹了在Javascript中如何判斷一個值是否為undefined,對大家的日常工作和學(xué)習(xí)很有幫助,下面來一起看看吧。2016-09-09JavaScript?中的?this?綁定規(guī)則詳解
這篇文章主要介紹了JavaScript?中的?this?綁定規(guī)則詳解的相關(guān)資料,需要的朋友可以參考下2023-02-02