Skip to main content

How to check if a css rule exists


I need to check if a css rule exists because I want to issue some warnings if a css file is not included.



What is the best way of doing this?



I could filter through window.document.styleSheets.cssRules but I'm not sure how cross-browser this is. (plus I notice on stackOverflow that object is null for styleSheet[0])



I would also like to keep dependencies to a minimum.



Is there a straightforward way to do this? Do I just have to create matching elements and test the effects?



Edit: If not, what are the cross-browser concerns of checking window.document.styleSheets?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I don't know if this is an option for you, but if it's a single file you want to check, then you can write your error message and toggle the style to hide it in that file.

    <span class="include_error">Error: CSS was not included!</span>


    CSS file:

    .include_error {
    display: none;
    visibility: hidden;
    }

    ReplyDelete
  2. I test for proper CSS installation using javascript.

    I have a CSS rule in my stylesheet that sets a particular id to position: absolute.

    #testObject {position: absolute;}


    I then programmatically create a temporary div with visibility: hidden with that ID and get the computed style position. If it's not absolute, then the desired CSS is not installed.



    If you can't put your own rule in the style sheet, then you can identify one or more rules that you think are representative of the stylesheet and not likely to change and design a temporary object that should get those rules and test for their existence that way.

    Or, lastly, you could try to enumerate all the external style sheets and look for a particular filename that is included.

    The point here is that if you want to see if an external style sheet is included, you have to pick something about that style sheet that you can look for (filename or some rule in it or some effect it causes).

    ReplyDelete
  3. If you are worried about not being able to edit other people's stylesheets, you can proxy them through a stylesheet of your own, using import

    @import url('http://his-stylesheet.css');
    .hideErrorMessage{ ... }


    This is enough if you just want to know if your code is trying to load the stylesheet but won't help if you need to know if the foreign stylesheet was then loaded correctly.

    ReplyDelete
  4. Here is what I got that works. It's similar to the answers by @Smamatti and @jfriend00 but more fleshed out. I really wish there was a way to test for rules directly but oh well.

    CSS:

    .my-css-loaded-marker {
    z-index: -98256; /*just a random number*/
    }


    JS:

    $(function () { //Must run on jq ready or $('body') might not exist

    var dummyElement = $('<p>')
    .hide().css({height: 0, width: 0})
    .addClass("my-css-loaded-marker")
    .appendTo("body"); //Works without this on firefox for some reason

    if (dummyElement.css("z-index") != -98256 && console && console.error) {
    console.error("Could not find my-app.css styles. Application requires my-app.css to be loaded in order to function properly");
    }
    dummyElement.remove();
    });

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.