Trying to insert css document with Javascript, however I receive error saying that request has to be CORS enabled. Is there a way to deal with it?
Here is the code:
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://use.fontawesome.com/releases/v5.2.0/css/all.css';
link.integrity = 'sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ';
link.crossorigin = 'anonymous';
document.head.appendChild(link);
The crossorigin attribute, valid on the <audio> , <img> , <link> , <script> , and <video> elements, provides support for CORS, defining how the element handles cross-origin requests, thereby enabling the configuration of the CORS requests for the element's fetched data.
An integrity attribute is used to allow the browser to check the file source to ensure that the code is never loaded if the source has been manipulated. Crossorigin attribute is present when a request is loaded using 'CORS' which is now a requirement of SRI checking when not loaded from the 'same-origin'.
The integrity attribute allows a browser to check the fetched script to ensure that the code is never loaded if the source has been manipulated. Subresource Integrity (SRI) is a W3C specification that allows web developers to ensure that resources hosted on third-party servers have not been altered.
Change crossorigin
(not a valid HTMLLinkElement
attribute) to crossOrigin
(note the capital "O"). Remember that HTML element properties are generally spelled in camel case (first word lower case with all subsequent words having their first letter capitalized) in Javascript.
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://use.fontawesome.com/releases/v5.2.0/css/all.css';
link.integrity = 'sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ';
link.crossOrigin = 'anonymous';
document.head.appendChild(link);
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