Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting link tag with integrity and crossorigin attribute issue

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);
like image 867
kkris1983 Avatar asked Aug 20 '18 21:08

kkris1983


People also ask

What is crossorigin attribute in HTML?

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.

What is integrity and crossorigin in link tag?

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

What is integrity attribute in link tag?

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.


1 Answers

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);
like image 117
Unmitigated Avatar answered Oct 19 '22 22:10

Unmitigated