Removing an expression set in a stylesheet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Ley

    #1

    Removing an expression set in a stylesheet

    Hi,

    IE has the ability to setExpressions on stylesheets so you can
    calculate the value of the css property through script.

    For various reasons I'm wanting to use a side-effect of this to attach
    an event to every element of a class in a document (I'm including
    content from a lot of large 3rd party content, and iterating over the
    entire DOM searching for the classes and then attaching the event is
    proving too slow, aswell as being too late post-load as the pages are
    long and interaction would ideally occur before onload fires, it's an
    embedded IE only solution.)

    The solution I'm using is:
    <URL: http://jibbering.com/2004/1/setExpression.html >

    <style type="text/css">
    .chicken {
    color:green;
    moomin:expressi on(snork(this)) ;
    }
    </style>
    <script type="text/jscript">
    statusCount=0;
    function snork(el) {
    el.onclick=hand ler;
    window.status=+ +statusCount;
    }
    function handler() {
    alert("Hi!");
    }
    </script>
    </head>
    <body>
    <p class="chicken" >
    A paragraph.
    </p>

    Obviously this is far from ideal as it means that the onclick handler
    is attached everytime the css is evaluated (and this is often!) so I
    need to remove the rule after attaching it. However I can't find a
    way of doing this!

    Changing the className of the objects to something else doesn't
    achieve it, playing with the style or runtimeStyle of the object
    doesn't, calling removeExpressio n on the elements style doesn't do
    anything. So nothing on the element itself seems to achieve it.

    Turning to the stylesheet, none of disabling the entire stylesheet,
    removing the rule, changing the cssText etc. seem to do anything and

    styleSheetRule. style.removeExp ression does not exist which would've
    been the assumed way of doing it.

    Does anyone know of a way? Setting the snork function to an empty
    function does make performance reasonable, but with hundreds of these
    classes on the page, it's still too slow - and I'm dealing with big
    pages which is why parsing the DOM is too slow.

    Jim.
    --
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

  • Richard Cornford

    #2
    Re: Removing an expression set in a stylesheet

    "Jim Ley" <jim@jibbering. com> wrote in message
    news:3ff4867b.6 959737@news.cis .dfn.de...
    <snip>[color=blue]
    >Obviously this is far from ideal as it means that the onclick
    >handler is attached everytime the css is evaluated (and this
    >is often!) so I need to remove the rule after attaching it.
    >However I can't find a way of doing this![/color]
    <snip>

    I couldn't find a way of removing/disabling the expression either and
    having added an additional 4 thousand odd chicken class paragraphs to
    the test page I could easily see how the expression was a problem as it
    renders the rest of the JScript execution sluggish to say the least
    (took ages for the handler function to put up the alert).

    However, I experimented with adding another styleSheet entry after the
    existing ones with a chicken class that would overload the -
    moomin:expressi on(snork(this)) ; - with - moomin:expressi on(void 0); -
    and when that worked it significantly improved the performance of the
    handler function on my modified test page.

    Using a test css file:-
    ---------------- expTest.css ----------------------
    ..chicken {
    moomin:expressi on(void 0);
    }
    --------------------------------------------------------
    - calling the IE createStyleShee t function in the onload handler:-

    document.create StyleSheet('exp Test.css');

    - or creating an additional link element with the "disabled" attribute
    set:-

    <link rel="stylesheet " href="expTest.c ss" type="text/css" disabled>

    -and then enabling it in the onload handler with:-

    document.styleS heets[2].disabled = false;

    - both seemed to broadly work, while creating a disabled STYLE element
    and then enabling it in the same way as the link element did not work at
    all.

    Unfortunately I can only describe this as broadly working because I
    notice that on initially loading/enabling the overloading
    moomin:expressi on CSS file with the onload handler caused some of the 4
    thousand odd paragraphs not to have the onclick handler set (the ones
    toward the end of the HTML). Re-loading the test page resulted in all of
    the paragraph onclick handlers being assigned and clearing IE's cache
    resurrected the initial problem. Microsoft's documentation implies that
    calling - document.recalc - should result in the expressions being
    recalculated for all elements (so assigning the onclick handler) but
    that did not help.

    Richard.


    Comment

    • Jim Ley

      #3
      Re: Removing an expression set in a stylesheet

      On Fri, 2 Jan 2004 21:08:23 -0000, "Richard Cornford"
      <Richard@litote s.demon.co.uk> wrote:
      [color=blue]
      >However, I experimented with adding another styleSheet entry after the
      >existing ones with a chicken class that would overload the -
      >moomin:express ion(snork(this) ); - with - moomin:expressi on(void 0); -
      >and when that worked it significantly improved the performance of the
      >handler function on my modified test page.[/color]

      hmm, I think that's equivalent in performance to my snork=function( )
      {} onload, and I still think that's noticeably a bit slow... It's
      very annoying it's such a neat solution...

      Cheers,

      Jim.
      --
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Richard Cornford

        #4
        Re: Removing an expression set in a stylesheet

        "Jim Ley" <jim@jibbering. com> wrote in message
        news:3ff6e0d3.9 432162@news.cis .dfn.de...
        <snip>[color=blue]
        >hmm, I think that's equivalent in performance to my
        >snork=function () {} onload, and I still think that's
        >noticeably a bit slow... It's very annoying it's such
        >a neat solution...[/color]

        I tried a version of - snork=function( ){}; - but I thought the void
        expression was still much quicker (probably because there is no overhead
        in calling the function).

        However, kicking the problem about a bit more revealed some possibly
        useful information. I had changed the status line report to:-

        window.status=+ +statusCount+' '+el.style.getE xpression("moom in");

        - and unsurprisingly that just appended "undefined" to the status
        report, until one of my experiments caused me to change the snork
        function to return a string (but any non-undefined return value seems to
        work). When I had done that I noticed that the first (4 thousand odd)
        call(s) to snork appended "undefined" but from then on "snork(this )"
        was appended. That means that with the first call to snork with a return
        value the expression is somehow attached to the P element's style
        object.

        That prompted me to try calling - removeExpressio n - on the style
        object. It appears that IE will not put up with - removeExpressio n -
        being called from within snork so I did a quick experiment with
        recording the P element's style objects in an array and then using
        onload to run through the array calling - removeExpressio n("moomin"); -
        on each recorded style object. That actually appears to work, but only
        in combination disabling - document.styleS heets[0]; -.

        This is the page I was testing with (I was testing locally so the URL
        for the external CSS is not the original, and I have cut most of the P
        elements from the test page):-

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html lang="en">
        <head>
        <title>removeEx pression Demo.</title>
        <style type="text/css">
        .chicken {
        color:green;
        moomin:expressi on(snork(this)) ;
        }
        </style>
        <link rel="StyleSheet " href="jibbering 2.css" type="text/css">
        <script type="text/jscript">
        var toRemoveExp = [];
        statusCount=0;
        function snork(el) {
        el.onclick=hand ler;
        window.status=+ +statusCount+' '+el.style.getE xpression("moom in");
        if(el.style.get Expression("moo min")){
        toRemoveExp[toRemoveExp.len gth] = el.style;
        }
        return 1;
        }
        function handler() {
        alert("Hi! "+this.style.ge tExpression("mo omin"));
        }
        window.onload=f unction() {
        var ss=document.sty leSheets[0];
        ss.disabled=tru e;
        for(var c = toRemoveExp.len gth;c--;){
        toRemoveExp[c].removeExpressi on("moomin");
        }
        }
        </script>

        </head>
        <body>
        <h1></h1>
        <p class="intro">A n example of removeExpressio n failing in IE6,
        this is an IE proprietary feature, so expect to see nothing
        much if you're using other browsers.</p>

        <div id="content">

        <p class="chicken" >
        A paragraph.
        </p>
        <p class="chicken" >
        Another paragraph.
        </p>
        <!-- Repeat the previous 6 lines 2000 times -->

        </div>
        <p class="footer"> Jim Ley - <a
        href="mailto:ji m@jibbering.com ">jim@jibbering .com</a>, <a
        href="/">Jibbering.com </a></p>
        </body>
        </html>

        There are more elegant and efficient ways of arranging which style
        objects to call - removeExpressio n - on, and I did notice that the
        problem with some of the final P elements no getting the onclck handler
        assigned on the initial load was still happening, but at least this does
        demonstrate that the expressions can be removed.

        Richard.


        Comment

        Working...