Inserting link tag with integrity and crossorigin attribute issue

☆樱花仙子☆ 提交于 2019-12-23 16:32:24

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/51938610/inserting-link-tag-with-integrity-and-crossorigin-attribute-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!