如何将跨域添加到< script>标签? [英] How to add just crossorigin to <script> tag?
本文介绍了如何将跨域添加到< script>标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅将交叉源添加到以下<script>
标记?
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.
当前输出:
<li><div id="somevalue"><script type="text/javascript" async src="some-source"></script></div></li>
所需的输出:
<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>
推荐答案
只需使用setAttribute即可:
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);
})();
顺便说一句,crossorigin属性并不意味着是空的: https://developer.mozilla.org/zh-CN/docs/Web/HTML/CORS_settings_attributes
By the way, crossorigin attribute is not meant to be empty : https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
这篇关于如何将跨域添加到< script>标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文