How do i go about detecting if cors is enabled on a html5 audio element's remote file through javascript?
I would like to output a visualizer attached to the AudioContext only IF the audio tag's remote src file has cors enabled.
You can either send the CORS request to a remote server (to test if CORS is supported), or send the CORS request to a test server (to explore certain features of CORS). Send feedback or browse the source here: https://github.com/monsur/test-cors.org.
CORS is off by default for security purposes.
CORS or Cross-Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs). Installing this add-on will allow you to unblock this feature. Please note that, when the add-on is added to your browser, it is inactive by default (toolbar icon is grey C letter).
CORS is a unique web technology in that it has both a server-side and a client-side component. The server-side component configures which types of cross-origin requests are allowed, while the client-side component controls how cross-origin requests are made.
Just request the file through javascript before loading the resource, and check the response headers for the access-control-allow-origin header.
If the resource has the header, update the tag's src attribute with the resources' URL.
jQuery example of checking headers from request:
var audioSource = 'mysite.com/audiosource.ogg';
$.get( audioSource, function( data, textStatus, request) {
var header = request.getResponseHeader('access-control-allow-origin');
if(typeof header !== 'undefined') {
$('#audiotag').attr('src', audioSource);
}
});
It's worth noting that if the access-control-allow-origin header is set and you're not requesting from the right domain you'll be forbidden from loading the resource anyway
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With