Created
August 7, 2013 20:34
-
-
Save jahanson/6178372 to your computer and use it in GitHub Desktop.
Uses XDR in IE and $.ajax in any other browser. Make sure to include JQuery & Modernizr for browser detection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var BrowserDetect = | |
{ | |
init: function () | |
{ | |
this.browser = this.searchString(this.dataBrowser) || "Other"; | |
this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown"; | |
}, | |
searchString: function (data) | |
{ | |
for (var i=0 ; i < data.length ; i++) | |
{ | |
var dataString = data[i].string; | |
this.versionSearchString = data[i].subString; | |
if (dataString.indexOf(data[i].subString) != -1) | |
{ | |
return data[i].identity; | |
} | |
} | |
}, | |
searchVersion: function (dataString) | |
{ | |
var index = dataString.indexOf(this.versionSearchString); | |
if (index == -1) return; | |
return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); | |
}, | |
dataBrowser: | |
[ | |
{ string: navigator.userAgent, subString: "Chrome", identity: "Chrome" }, | |
{ string: navigator.userAgent, subString: "MSIE", identity: "Explorer" }, | |
{ string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, | |
{ string: navigator.userAgent, subString: "Safari", identity: "Safari" }, | |
{ string: navigator.userAgent, subString: "Opera", identity: "Opera" } | |
] | |
}; | |
BrowserDetect.init(); | |
var url = "https://secure.havenforhope.org/api/blog/?apikey=PqJmvTiwZmCBRmcLHYKbqkkmd"; | |
console.log(BrowserDetect.browser + " " + BrowserDetect.version); | |
if (BrowserDetect.browser == 'Explorer'){ | |
// Use Microsoft XDR | |
var req = new XMLHttpRequest(); | |
req.open('GET', url,false); | |
req.send(null); | |
var headers = req.getAllResponseHeaders().toLowerCase(); | |
alert(headers); | |
alert(JSON.stringify(req.responseText)); | |
}else{ | |
$.ajax({ | |
type: "GET", | |
url: url, | |
dataType: "json", | |
success: function(response) { | |
console.log("Response: "+ JSON.stringify(response)); | |
}, | |
error: function (msg) { | |
console.log(JSON.stringify(msg)); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment