How to add just crossorigin to <script> tag?

你说的曾经没有我的故事 提交于 2019-12-13 04:36:56

问题


How can I add just crossorigin to the following <script> tag?

The goal is to only add the crossorigin and not added the anonymous or use-credentials attributes.

Current output:

<li><div id="somevalue"><script type="text/javascript" async src="some-source"></script></div></li>

Desired output:

<li><div id="somevalue"><script type="text/javascript" crossorigin async src="some-source"></script></div></li>

(function() {
  var scriptTag = document.createElement('script');
  scriptTag.type="text/javascript";
  scriptTag.async=true;
  scriptTag.src="some-source";
  
  var divTag = document.createElement('div');
  divTag.id='somevalue';
  divTag.append(scriptTag);
  
  var liTag = document.createElement('li');
  liTag.append(divTag);
  
  var ulList = document.getElementById('footer-menu');
  ulList.append(liTag);
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="menu" id="footer-menu">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

回答1:


Simply use setAttribute :

(function() {
  var scriptTag = document.createElement('script');
  scriptTag.type="text/javascript"
  scriptTag.async=true
  scriptTag.setAttribute('crossorigin','anonymous')
  scriptTag.src="some-source";

  console.log(scriptTag);
})();

By the way, crossorigin attribute is not meant to be empty : https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes



来源:https://stackoverflow.com/questions/50420726/how-to-add-just-crossorigin-to-script-tag

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