Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if cors is enabled?

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.

like image 869
vimes1984 Avatar asked Mar 06 '16 17:03

vimes1984


People also ask

How do you check if CORS is 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.

Is CORS enabled by default?

CORS is off by default for security purposes.

Is CORS enabled by default in Chrome?

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).

Is CORS on the server or client?

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.


1 Answers

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

like image 172
glcheetham Avatar answered Sep 27 '22 21:09

glcheetham