-2

I have an element that has the following styles:

<div id="myElement" style="opacity: 0.9; cursor: auto; visibility: visible;"></div>

I want to make sure it always (setInterval?) has these styles, no more no less, NO changes at all. I wonder if its possible to retrive all styles of an element (inline CSS or external) and compare compare them to make sure there were not changes?

eozzy
  • 66,048
  • 104
  • 272
  • 428

1 Answers1

1

For a single element I would not imagine that the performance difference between setting it and checking it first is significant. You might as well just do something like this:

$(document).ready(function(){
    var t = setInterval(function(){
        $('#myElement').attr('style', 'opacity: 0.9; cursor: auto; visibility: visible;');
    }, 1000);
}

I realise that you would normally set the styles using $('#myElement').css, but as you want to ensure no new styles were added, setting the style attribute should achieve what you want. Unfortunately it doesn't account for changes to the stylesheet or style blocks elsewhere on the page. To do this you would have to make your style attribute a bit more comprehensive and include "!important" after each value.

Out of interest, why do you need to do this? It sounds like there might be a better solution to the problem.

Edit: If you want to stop the user using clearInterval, instead of

var t = setInterval(...);

Just use

setInterval(...);

As clearInterval requires the reference to the interval in order to clear it (correct me if I'm wrong here). By not creating that reference the interval is still executed but not clearable.

Ultimately though I don't think there will be a fool proof method to achieve this. It should however, prevent all but the most determined users with from hiding whatever it is you want them to see.

Edit 2: If you just want to check the CSS it is possible but a bit of a pain as you would have to check each property in turn. Using jQuery's css function you could do something like this:

$(document).ready(function(){
    setInterval(function(){
        var el = $('#myElement');
        var tampered = false;
        if (el.css('display') != 'block') tampered = true;
        if (el.css('visibility') != 'visible') tampered = true;
        if (el.css('position') != 'static') tampered = true;
        ....
        if (tampered){
            // Do your thing
        }
    }, 1000);
}
Maloric
  • 5,525
  • 3
  • 31
  • 46
  • The objective here is to make sure the user doesn't override the CSS and hide the element somehow (display:none, position:absolute etc..) – eozzy May 08 '13 at 15:40
  • setInterval() will not work because user can use clearInterval()? – DmitryK May 08 '13 at 15:42
  • And setting `style` won't work because the user can inject a stylesheet that adjusts different styles or uses `!important`. – Quentin May 08 '13 at 15:43
  • Even if the user uses !important in an injected stylesheet, adding !important to the inline styles will always give them precedent. – Maloric May 08 '13 at 15:46
  • I'm not looking to play cat and mouse, if changes detected, I'll crash the browser :) http://stackoverflow.com/questions/10965987/how-do-you-force-chrome-pages-tabs-to-crash-using-javascript – eozzy May 08 '13 at 15:47
  • Instead of setting these CSS properties, can't I detect changes? Cause the CSS is already set, I just need to detect changes and do X – eozzy May 08 '13 at 15:56
  • Updated my answer to show how to just read the css that has been applied. – Maloric May 08 '13 at 16:05
  • @Maloric — Still not sufficient. There are many, many properties that could be added that would remove the element from sight. – Quentin May 08 '13 at 16:10
  • @Quentin I'm open to better suggestions. As I said, I don't believe there is a bullet proof method. For each one of those properties, add another line. I've listed three of the major ones. You could also try width, height, opacity to name a few. – Maloric May 08 '13 at 16:13
  • I'm using a different approach. I'm checking if the element is within the viewport, and is visible, if not .. do X – eozzy May 08 '13 at 18:44