Cross-origin resource sharing (CORS) เป็นกลไกอนุญาตให้เข้าถึง restricted resources จากโดเมนอื่นในเว็บเบราว์เซอร์
เช่น หากคุณคลิก HTML5 video player ใน demo sections จะขอ permission กล้อง ถ้าผู้ใช้อนุญาตจึงจะเปิดกล้องได้
Chrome, Firefox, Opera, Safari ใช้ XMLHttprequest2 object IE ใช้ XDomainRequest object
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}