1 (function () { 2 function form2httpAuth () { 3 /* Skip if the browser indicates it does this itself. */ 4 if (document.implementation && 5 document.implementation.hasFeature && 6 document.implementation.hasFeature("HTTPFormAuth", "1.0")) 7 return true; 8 9 /* Attempt login with provided credentials. */ 10 var username = this.username.value; 11 var password = this.password.value; 12 var authed = false; 13 var form = this; 14 $.ajax({ 15 url: "/ajax-login.php", 16 /* synchronous hangs some browsers temporarily. :/ */ 17 async: false, 18 global: false, 19 username: username, 20 password: password, 21 dataType: 'text', 22 success: function (data, status, xhr) { 23 authed = true; 24 }, 25 error: function (xhr, status, err) { 26 authed = false; 27 }, 28 complete: function (xhr, status) { 29 /* Don't send username and password if we successfully managed to auth via HTTP */ 30 if (authed) { 31 form.username.parentNode.removeChild(form.username); 32 form.password.parentNode.removeChild(form.password); 33 } 34 } 35 }); 36 37 /* This is the only way to get browsers to submit the form exactly as 38 the user did, which is why we aren't using async above. */ 39 return true; 40 } 41 42 $("form.http-authentication").submit(form2httpAuth); 43 })();