Skip to content

Instantly share code, notes, and snippets.

@KushalP
Created October 20, 2011 12:58
Show Gist options
  • Select an option

  • Save KushalP/1301082 to your computer and use it in GitHub Desktop.

Select an option

Save KushalP/1301082 to your computer and use it in GitHub Desktop.
Capture the 401 Authentication code
// A lazy raw JS function to quickly play with this.
function getHTTPObject() {
if (typeof XMLHttpRequest != 'undefined') {
return new XMLHttpRequest();
}
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
return false;
}
window.onload = function()
{
var http = getHTTPObject();
if (http) {
// You'll need to fill out url, user, and pass manually.
// It breaks somewhere between this line and the next. It does
// however correctly catch the Auth dialog. It just can't go
// passed it.
this.http.open("get", url, false, user, pass);
this.http.send("");
if (http.status == 200) {
// do something as the url has been authed
} else {
alert("Incorrect username and/or password.");
}
} else {
alert("Couldn't build HTTPObject.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment