javascript modification of IE's CSS expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Csaba Gabor

    #1

    javascript modification of IE's CSS expression

    How, using javascript, can you set or change a CSS value
    from something to something else which has the IE
    proprietary format: expression(java sriptCode)?


    I'd like to know the answer in general, but the specific
    issue motivating this is that I have an image in a TD
    which I am able to nicely align left/right/center/top
    bottom/middle and set (stretch) to various sizes.

    I'd like to be able to set the image size as a percent
    of the enclosing TD (and other more complicated
    scenarios). In IE, setting imgElem.style.h eight
    makes the image get stuck to the top (though sized
    correctly).

    So I have the idea that I could make imgElem.height be
    an expression for IE and calculate the actual height
    that I want the image to be. Now, I agree that I
    could store the necessary multiplicative factor in
    a window.heightFa ctor variable, or better yet, have a
    dummy function that the expression points to (as in
    the example below), but is that really the best way
    to go???


    Any ideas?
    Thanks,
    Csaba Gabor from Vienna

    PS, for those who haven't seen it, here's a simple
    example of this expression business (only works with IE):


    <body style="margin:0 ">
    <script type='text/javascript'>
    function process(value) {
    if (value.charAt(v alue.length-1)!="%")
    return value;
    var img=document.ge tElementById('i mg');
    return(img.offs etParent.offset Height *
    value.substring (0,value.length-1) / 100);
    }
    </script>
    <form action=''>
    <table border style="height:1 00%">
    <tr><td style="height:5 %">Enter ratio:
    <input type=text name=foo></td></tr>

    <tr><td valign=bottom align=center><i mg id=img
    style="height:e xpression(proce ss(document.for ms[0][0].value))"
    src='pic.jpg'></td></tr>
    </form>
    </body>

    Note that if you leave out the 'process' in that expression, then as
    you put in the '%' of 50% that the image will be sized at 50%, but
    blasted to the top of the TD.
  • Csaba Gabor

    #2
    Re: javascript modification of IE's CSS expression

    The following script doesn't answer my question of
    how to set an expression(java scriptExpressio n) onto
    a style element, but I think it is a little more
    instructive than my previous example. Note that the
    expression has nothing to couple it to the variable
    window.fooVal. Indeed, even the 'process' function
    is missing it and yet the updating is still immediate
    (type in: 50%). This would seem to indicate a huge
    amount of computational overhead. I'm supposing
    that if any events trigger, expressions reevaluate.

    Csaba

    <body style="margin:0 ">
    <script type='text/javascript'>
    var fooVal="200";
    function process() {
    var value = window['foo'+'Val'];
    if (value.charAt(v alue.length-1)!="%") return value;
    var img=document.ge tElementById('i mg');
    return(img.offs etParent.offset Height *
    value.substring (0,value.length-1) / 100);
    }
    </script>
    <form action=''>
    <table border style="height:1 00%">

    <tr><td style="height:5 %">Enter ratio:
    <input type=text
    name=foo onPropertyChang e="window.fooVa l=this.value"></td></tr>

    <tr><td valign=bottom align=center><i mg id=img
    style="height:e xpression(proce ss())" src='pic.jpg'></td></tr>

    </table>
    </form>
    </body>

    Comment

    • Csaba Gabor

      #3
      Re: javascript modification of IE's CSS expression

      OK, I finally have it - setExpression allows
      you to set an expression on a style property
      on the fly with javascript (see also
      removeExpressio n and document.recalc ):


      <body style="margin:0 " onLoad="init()" >
      <script type='text/javascript'>
      var tbVal="200";
      function elem(id) {
      return document.getEle mentById(id);}
      function process() {
      var value = window.tbVal;
      if (value.charAt(v alue.length-1)!="%") return value;
      var img=document.ge tElementById('i mg');
      return(img.offs etParent.offset Height *
      value.substring (0,value.length-1) / 100);
      }

      function init() {
      var iStyl = elem('img').sty le;
      iStyl.setExpres sion("height"," process()");
      elem('tbIn').fo cus();
      }
      </script>
      <form action=''>
      <table border style="height:1 00%">

      <tr><td style="height:5 %">Enter ratio:
      <input type=text tabIndex=1 id=tbIn name=tbIn
      onPropertyChang e="window.tbVal =this.value"></td></tr>

      <tr><td valign=bottom align=center><i mg id=img
      src='pic.jpg'></td></tr>

      </table>
      </form>
      </body>


      Csaba Gabor from Vienna

      Comment

      • Alvaro G Vicario

        #4
        Re: javascript modification of IE's CSS expression

        *** Csaba Gabor wrote/escribió (Mon, 14 Mar 2005 23:25:29 GMT):[color=blue]
        > id=img
        > style="height:e xpression(proce ss(document.for ms[0][0].value[/color]

        I'd try something like:

        document.getEle mentById('img') .style.height=d ocument.forms[0][0].value+'%';

        If it works, you can get rid of the proprietary IE extension. If you're to
        publish a web site that only works in Windows IE, you'd better write a real
        app in VisualBasic or C++ ;-)


        --
        -- Álvaro G. Vicario - Burgos, Spain
        -- Don't e-mail me your questions, post them to the group
        --

        Comment

        Working...