क्रॉस-ओरिजिन रिसोर्स शेयरिंग (CORS) एक ऐसा तंत्र है जो वेब ब्राउज़र में किसी अन्य डोमेन से प्रतिबंधित संसाधनों की अनुमति देता है।
उदाहरण के लिए, यदि आप HTML5 डेमो सेक्शन में HTML5 वीडियो प्लेयर पर क्लिक करते हैं, तो यह कैमरा अनुमति मांगेगा। यदि उपयोगकर्ता अनुमति देता है, तो ही यह वेब एप्लिकेशन के लिए कैमरा खोलेगा, अन्यथा नहीं।
यहां Chrome, Firefox, Opera और Safari सभी XMLHttpRequest2 ऑब्जेक्ट का उपयोग करते हैं और Internet Explorer समान XDomainRequest ऑब्जेक्ट का उपयोग करता है।
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
}
else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}