简体   繁体   English

使用 require js 向脚本标签添加 crossorigin 属性

[英]add crossorigin attribute to script tag with require js

here my problem, I call a lot of script with requirejs and for one of them (and ONLY one) I need to add crossorigin attribute to make my script call look like that这是我的问题,我使用 requirejs 调用了很多脚本,对于其中一个(并且只有一个),我需要添加 crossorigin 属性以使我的脚本调用看起来像这样

<script type="text/javascript" charset="utf-8" async 
data-requirecontext="_" data-requiremodule="myModule" 
src="cdn.moduleUrl" crossorigin="anonymous"></script>

I've already search on docs, google and stackOverflow but I could'n find any response.我已经在文档、谷歌和 stackOverflow 上搜索过,但我找不到任何回复。

PS: I use requirejs 2.3.5 PS:我使用requirejs 2.3.5

Ok, so I finnaly found a "proper" solution to my problem, in require code, you have an undocumented option onNodeCreated to set a callback before inserting script tag on dom so with this code, I achieve my goal !好的,所以我最终找到了解决我的问题的“正确”解决方案,在 require 代码中,您有一个未记录的onNodeCreated选项来在 dom 上插入脚本标记之前设置回调,因此使用此代码,我实现了我的目标!

require.config({
  paths: {
    'myModule': 'cdn.moduleUrl'
  },
  attributes: {
    "myModule": {
      crossorigin: "anonymous"
    }
  }, 
  onNodeCreated: function(node, config, name, url){
    if(config.attributes && config.attributes[name]){
      Object.keys(config.attributes[name]).forEach(attribute => {
        node.setAttribute(attribute, config.attributes[name][attribute]);
      });
    }
  }
});

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

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