How to include external css and javascript in riot tag?

I want to create a tag for a carousel, but I only want to load CSS and Javascript files when this tag is used on the site, but I can only make it work if I add it to <body>from my site.

Is there a way to download only in the tag?

my example tag (this only works if I move the css and .js files to the body of the website:

<carousel>
  <div class="row" hide="{$gg.filterApplied || $gg.redeem.redeemInProgress()}">
    <div class="col-sm-12">
      <div class="logos text-center">
        <div class="divider"></div>
        <div data-slick='{"slidesToShow": 4, "slidesToScroll": 4}'>
          <div><h3>1</h3></div>
          <div><h3>2</h3></div>
          <div><h3>3</h3></div>
          <div><h3>4</h3></div>
          <div><h3>5</h3></div>
          <div><h3>6</h3></div>
        </div>
        <div class="divider"></div>
      </div>
    </div>
  </div>

  <style scoped>
    @import url("//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css")
  </style>

  <script src="//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js"></script>
  <script>
    var self = this;
    self.mixin(SharedMixin)
  </script>
</carousel>
+4
source share
1 answer

I had a similar problem and solved it this way.

I added a label method using mixin to load resources at runtime, and I call it before starting, passing a list of css and js files.

It uses the getScript jQuery function.

loadResources(css, js, callback){
    for(var i = 0; i < css.length; i++){
        if(css[i] !== ''){
            var link = document.createElement('link');
            link.setAttribute("rel", "stylesheet");
            link.setAttribute("type", "text/css");
            link.setAttribute("href", css[i]);
            document.getElementsByTagName("head")[0].appendChild(link);
        }
    }
    var jsProgress = 0;
    for(var j = 0; j < js.length; j++){
        $.getScript(js[j], function () {
            if (++jsProgress == js.length && typeof callback !== 'undefined') callback();
        });
    }
}
+1
source

Source: https://habr.com/ru/post/1649215/


All Articles