4

We are creating a new widget in that we are implementing a feature which tells how many css files which are included in a html page.

We need the count of css files used in the page and also the names of the css files.

I am bit new to angular and I have heard this can be done using directives but have no idea how to achieve it.

Need your help guys. Thanks in advance.

3 Answers 3

2

Is your application already in Angular? This jQuery code would do this task easily:

var cssList = []
$('head link[type="text/css"]').each(function(){
    cssList.push($(this).attr("href"));
});
console.log("List of files:" + cssList.join());
console.log("Number of files:" + cssList.length);
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer but as I mentioned in my question we want this to be implemented in angular rather than in jQuery. Can we have this code in directive way?
A directive is usually a UI component... you should use a service instead. Angular has jqLite, so you can use the same code without needing to have jQuery imported see: angular element documentation
Hello Lucas Rodrigues! I just got this edit in review. Unfortunately I had to reject it. Things like "thanks" or "hi" are considered noise, and should be removed from posts, not added to them. If you're interested, the relevant discussion is here.
1

Using angular:

  .directive('cssCounter', ['$document', function($document) {

    function cssFilter(l) {
      return l.type === 'text/css' || l.rel === 'stylesheet';
    }

    function link(scope, element, attrs) {

      var links = $document[0].querySelectorAll('link');
      var linksAsArray = Array.prototype.slice.call(links, 0);
      var cssLinks = linksAsArray.filter(cssFilter);
      var cssNames = cssLinks.map(function(c) {
        return c.href;
      });


      scope.cssFiles = cssNames;
    }

    return {
      link: link,
      template: '<p>CSS count: {{cssFiles.length}}<ul><li ng-repeat="c in cssFiles">{{c}}</li></ul></p>'
    };
  }]);

And example plunker can be found here: http://plnkr.co/edit/TqNAOKw4biyA7akdHfa4

3 Comments

That's better than what I've got. Could you please check what I've got wrong? plnkr.co/edit/l0Yvgn0PuVZawmiXK4a7?p=info
@LucasRodrigues you should open up a new question. However, in your directive, you are looking for type="text/css", but in your html, you specify rel="stylesheet".
Oh, that's why I was getting an empty array! Plunkr imported those for me, and I didn't pay attention to that. Thanks!
0

Using no external libraries, this is how you can do it:

var links = document.querySelectorAll('link');
var linksAsArray = Array.prototype.slice.call(links, 0);
var cssLinks = linksAsArray.filter(function(l){return l.type === 'text/css';});
var cssNames = cssLinks.map(function(c){return c.href;});
var count = cssNames.length;
console.log("css count and names:", count, cssNames);

1 Comment

thanks for the answer, but I need this implemented in angular js, let me know how to convert the above into angular js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.