利用JS實現表單的自動認可

來源:互聯網
上載者:User

標籤:style   blog   java   color   width   2014   

        今天需要將一個chat整合到客戶的網站上去,實現網站的註冊使用者登入本網站之後點擊某個連結能夠直接登入到chat上去。我保留了chat原有的登入介面,採用JS技術當頁面跳轉過來的時候自動填滿表單,並自動認可表單,從而實現網站使用者無需再次登入即可進入chat。具體代碼實現如下

JS代碼

<script> //取得cookie值 function getCookie(name){    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");     if(arr=document.cookie.match(reg)){        return unescape(arr[2]);     }else{        return null;     } } //表單自動認可函數function tijiao(){      function sub(){      var a = document.loginForm.elements; //取得表單下所有元素     a[2].value = getCookie('username');      a[3].value = getCookie('username');      //a[4].value = getCookie('channel');      document.loginForm.submit();     }setTimeout(sub,0);//以毫秒為單位的.1000代表一秒鐘.根據你需要修改這個時間. }//點擊button提交表單function clickbutton(obj){     obj.onclick = function(){         obj.submit();     } }</script>

HTML代碼

<form id="loginForm" name="loginForm" action="[LOGIN_URL/]" method="post" enctype="application/x-www-form-urlencoded">    <div id="loginFormContainer" style="margin:0px;padding-bottom: 10px;">        <input type="hidden" name="login" id="loginField" value="login"/>        <input type="hidden" name="redirect" id="redirectField" value="[REDIRECT_URL/]"/>        <div><label for="userNameField">[LANG]userName[/LANG]:</label>        <input type="text" name="userName" id="userNameField" maxlength="[USER_NAME_MAX_LENGTH/]"/></div><br />        <div><label for="passwordField">[LANG]password[/LANG]*:</label>        <input type="password" name="password" id="passwordField"/><span style="margin-left:4px; color: #339900;">預設是您的使用者名稱</span></div><br />        <div><label for="channelField">[LANG]channel[/LANG]:</label>        <select name="channelName" id="channelField">[CHANNEL_OPTIONS/]</select></div><br />        <div><label for="languageSelection">[LANG]language[/LANG]:</label>        <select id="languageSelection" name="lang" onchange="ajaxChat.switchLanguage(this.value);">[LANGUAGE_OPTIONS/]</select></div><br />        <!--        <div><input type="submit" name="submit" style="width: 240px;height: 50px;background: url(../chat/img/bglogin2.png);background-repeat: no-repeat;border: 0px;" id="loginButton" value=""/></div>        -->        <div><button name="subButton" onclick="clickbutton(this);" style="width: 240px;height: 50px;background: url(../chat/img/bglogin2.png);background-repeat: no-repeat;border: 0px;" id="loginButton" value=""></button></div>    </div></form>

PS:1. 要在body中加入onload="tijiao();"2. 另外表單的提交按鈕(submit)需要隱去,不然會和js的submit()方法衝突,這裡採用button提交,避免在自動登入失敗時沒有提交按鈕


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.