Skip to content

Instantly share code, notes, and snippets.

@rparaujo
Created January 24, 2018 11:28
Show Gist options
  • Save rparaujo/d404d1abd5b8864e4547f29461208eef to your computer and use it in GitHub Desktop.
Save rparaujo/d404d1abd5b8864e4547f29461208eef to your computer and use it in GitHub Desktop.
Test if CORS is enabled
<html>
<head>
<title>Test for CORS</title>
<script type="text/javascript">
//Modified from the code found at test-cors.org
function testcors(url){
var createCORSRequest = function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Most browsers.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// IE8 & IE9
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
};
var method = 'GET';
var xhr = createCORSRequest(method, url);
xhr.onload = function() {
// Success code goes here.
thendothis(true);
};
xhr.onerror = function() {
// Error code goes here.
thendothis(false);
};
xhr.send();
//return iscors;
}
function thendothis(iscors){
var resptxt = "No";
if (iscors) { resptxt = "Yes"; }
document.getElementById("res").innerHTML = resptxt;
}
function runtest(frm){
testcors(frm.url.value);
}
</script>
</head>
<body>
<form>
URL: <input type="text" name="url" id="url" style="width:500px" /></br> <input type="button" value="Test if CORS" onclick="runtest(this.form)" />
</form>
<div>URL is CORS: <span id="res"></span></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment