views:

172

answers:

3

As the question implies, can I add a JQuery script inside an css file as an expression? Or something like:

.parent{
 background-color: pink;
 position:relative;
 width: 100%;
}

.child{
 position: absolute;
 background: transparent url('./background.gif') no-repeat top right;
 /*height: 100%;*/
 height:  expression($(this).parent.height()+'px');
 width: 12px;

}

the whole idea of this script is to solve the 100% div height in IE6.

+3  A: 

You cannot do it that way.

You could perform some kind of javascript when the page loads... (A function that looks for elements with the 100% height and replace that with the parent's height.)

Jongsma
so is there any way to solve it???
Affar
Like I said, you can perform javascript after (or during, but I wouldn't recommend that) the page has loaded. I don't know whether jQuery has any built in functions for that, but I personally use this function: http://simonwillison.net/2004/May/26/addLoadEvent/. Then, just perform the function you wrote on all .child elements. (You can get those elements with $('.child') in jQuery I believe.)
Jongsma
A: 

Not on any browser I tried; There are CSS expressions that might work on IE 6, but they're not exactly speedy (so says the link). I think the problem is when the CSS loads vs when the jQuery selector is initialized. So you'll have to write some on DOM ready code to select all of the elements that need their heights set and then set it.

dlamblin
A: 
#element {
    height: expression((function(element) {
              var x = window.setInterval (function () {
                    if (document.readyState.search (/loaded|complete/) > -1) {
                      window.clearTimeout (x);
                      $(element).height($(element).parent.height())
                    }
                  }, 50);
            })(this));
}

Let me explain: Firstly, expressions are bad. Follow dlablin's link to read why. You should consider other methods. If you must, the above snippet works as follows:

  1. The expression is first evaluated as soon as the CSS is read. That means, the DOM is not yet ready and jQuery is probably not yet loaded.

  2. Set as expression an anonymous function, that is instantaneously executed.

  3. This function delivers the current element as element to the containing code.

  4. We set an interval: Check every 50ms, if the DOM is ready. If it is, clear the interval and run the jQuery.

Remark: If you do this with lots of elements, this will really slow down your page. To emphasize this again: Don't do this at home, kids!

Boldewyn