简体   繁体   English

如何将交叉源添加到<script> tag?

[英]How to add just crossorigin to <script> tag?

How can I add just crossorigin to the following <script> tag? 如何仅将交叉源添加到以下<script>标记?

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> 

Simply use setAttribute : 只需使用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 顺便说一句,crossorigin属性并不意味着是空的: https : //developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM