javascript type casting nightmare IE7/firefox.

Discussion in 'Javascript' started by The Natural Philosopher, Nov 22, 2008.

  1. I've spent nearly two days and I cannot resolve this.
    All I want to do is compare the z-index of an element with an argument
    passed to a function. If its greater than the argument plus one, I want
    the test to pass.


    In firefox/safari, this works

    if ((level+1) <sample.style.zIndex )

    In IE7 it doesn't.

    I can mimic the same behaviour in safari/firefox IE7 by explicitly
    casting the objects to numbers using 'Number()'

    i.e

    if (Number(level+1) <Number(sample.style.zIndex) )

    fails equally on all platforms.

    So does
    if ((Number(level)+1) <Number(sample.style.zIndex) )

    So does
    if ((Number(level)+1) < (sample.style.zIndex))

    so does
    if (++level <sample.style.zIndex )

    and

    if ((++level) < sample.style.zIndex)

    I am sure I am missing something glaringly obvious, so please enlighten me.
     
    The Natural Philosopher, Nov 22, 2008
    #1
    1. Advertising

  2. The Natural Philosopher

    Evertjan. Guest

    The Natural Philosopher wrote on 22 nov 2008 in comp.lang.javascript:

    > I've spent nearly two days and I cannot resolve this.
    > All I want to do is compare the z-index of an element with an argument
    > passed to a function. If its greater than the argument plus one, I want
    > the test to pass.
    >
    >
    > In firefox/safari, this works
    >
    > if ((level+1) <sample.style.zIndex )
    >
    > In IE7 it doesn't.
    >


    This works fine on IE7:

    =====================================
    <div id='d1' style='position:absolute;z-index:5;'>
    xxxxxxxx</div>
    yyyyyyyyyyyyy

    <script type='text/javascript'>

    var level = 3;
    var d1 = document.getElementById('d1');
    alert(level>d1.style.zIndex)

    </script>
    =====================================

    > I am sure I am missing something glaringly obvious, ...


    Wonder, o wonder, we agree!

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 22, 2008
    #2
    1. Advertising

  3. Evertjan. wrote:
    > The Natural Philosopher wrote on 22 nov 2008 in comp.lang.javascript:
    >
    >> I've spent nearly two days and I cannot resolve this.
    >> All I want to do is compare the z-index of an element with an argument
    >> passed to a function. If its greater than the argument plus one, I want
    >> the test to pass.
    >>
    >>
    >> In firefox/safari, this works
    >>
    >> if ((level+1) <sample.style.zIndex )
    >>
    >> In IE7 it doesn't.
    >>

    >
    > This works fine on IE7:
    >
    > =====================================
    > <div id='d1' style='position:absolute;z-index:5;'>
    > xxxxxxxx</div>
    > yyyyyyyyyyyyy
    >
    > <script type='text/javascript'>
    >
    > var level = 3;
    > var d1 = document.getElementById('d1');
    > alert(level>d1.style.zIndex)
    >
    > </script>
    > =====================================
    >
    >> I am sure I am missing something glaringly obvious, ...

    >
    > Wonder, o wonder, we agree!
    >


    I traced it down to two bugs.

    Or whatever

    One was in my code.

    Firefox/safari made level+1 when level = 4 give '41'
    IE made it '5' My code was bad, and '41' worked.

    When I went 'all numbers' everything broke.

    So I fixed my code, and everything works.

    HOWEVER, beware. IE7 implicit casting is not identical to Firefox.

    According to my manual if you have (a > b+1)

    Both should be converted to numbers if at all possible.

    Now IE7 says that b+1 is numeric, and does the b+1 arithmetically.

    Firefox says that b is a string, and tacks a one on the end, then
    converts to a number. Then compares.
     
    The Natural Philosopher, Nov 22, 2008
    #3
  4. The Natural Philosopher

    SAM Guest

    Le 11/22/08 3:36 PM, The Natural Philosopher a écrit :
    > I've spent nearly two days and I cannot resolve this.
    > All I want to do is compare the z-index of an element with an argument
    > passed to a function. If its greater than the argument plus one, I want
    > the test to pass.


    Is it possible to know by JS the z-index of an element ?
    (if it hasn't be assigned first (by JS too))

    > In firefox/safari, this works
    >
    > if ((level+1) <sample.style.zIndex )


    if(sample.style.zIndex) alert(sample.style.zIndex > (+level+1));
    else alert('no sample z-index');

    > In IE7 it doesn't.


    Sure ?

    All is working as expected with :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta name="License"
    content="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.fr">
    <title>Untitled</title>
    <style type="text/css">
    div { border: 1px solid; padding:30px; float:left; margin:10px }
    p { clear: left }
    </style>
    <script type="text/javascript">
    var niv = function(sample, level)
    {
    sample = document.getElementById('d'+sample);
    if(sample.style.zIndex) alert(sample.style.zIndex > (+level+1));
    else alert('no sample z-index');
    }
    </script>
    </head>
    <body>
    <h1></h1>
    <div style="z-index: 3" id="d1">sample 1 z-index 3</div>
    <div style="z-index: 3" id="d2">sample 2 z-index 3</div>
    <div style="z-index: 2" id="d3">sample 3 z-index 2</div>
    <div id="d4">sample 4 z-index null</div>
    <p><a href="javascript:niv(1,1)">div 1 - level 1</a></p>
    <p><a href="javascript:niv(2,4)">div 2 - level 4</a></p>
    <p><a href="javascript:niv(3,3)">div 3 - level 3</a></p>
    <p><a href="javascript:niv(4,1)">div 4 - level 1</a></p>
    </body>
    </html>

    --
    sm
     
    SAM, Nov 22, 2008
    #4
  5. SAM wrote:
    > Le 11/22/08 3:36 PM, The Natural Philosopher a écrit :
    >> I've spent nearly two days and I cannot resolve this.
    >> All I want to do is compare the z-index of an element with an argument
    >> passed to a function. If its greater than the argument plus one, I
    >> want the test to pass.

    >
    > Is it possible to know by JS the z-index of an element ?
    > (if it hasn't be assigned first (by JS too))
    >


    Oh yes. Certainly.


    Anyway, if you missed it, the bug was in my code, BUT teh inplicit
    casting of IE7/Firefor is different.

    IE7 seems to say 'if no rules seem to cover this, assume a number'
    whereas firefox and safari assume a string.

    I haven't tested it, but it resolves down to

    var a='4';

    function test(s)
    {
    alert (s+1);

    giving 5, or 41, depending.

    }
     
    The Natural Philosopher, Nov 22, 2008
    #5
  6. The Natural Philosopher

    David Mark Guest

    On Nov 22, 11:05 am, The Natural Philosopher <> wrote:
    > SAM wrote:
    > > Le 11/22/08 3:36 PM, The Natural Philosopher a écrit :
    > >> I've spent nearly two days and I cannot resolve this.
    > >> All I want to do is compare the z-index of an element with an argument
    > >> passed to a function. If its greater than the argument plus one, I
    > >> want the test to pass.

    >
    > > Is it possible to know by JS the z-index of an element ?
    > > (if it hasn't be assigned first (by JS too))

    >
    > Oh yes. Certainly.


    No, the answer is maybe.

    >
    > Anyway, if you missed it, the bug was in my code, BUT teh inplicit


    What a shock.

    > casting of IE7/Firefor is different.


    Really? Do tell.

    >
    > IE7 seems to say 'if no rules seem to cover this, assume a number'
    > whereas firefox and safari assume a string.


    You need to stop posting nonsense here. What is the point?

    >
    > I haven't tested it, but it resolves down to
    >


    Try testing code before you post it. If you had...

    [snip untested and malformed code]
     
    David Mark, Nov 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Jahn

    Void * type tracking nightmare!!!

    Gordon Jahn, Jan 31, 2004, in forum: C++
    Replies:
    2
    Views:
    323
    Gianni Mariani
    Jan 31, 2004
  2. heyo
    Replies:
    3
    Views:
    962
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    831
    Dan Pop
    Apr 2, 2004
  4. Jeff Louella

    IE7 on XP vs IE7 on Vista

    Jeff Louella, Mar 1, 2007, in forum: HTML
    Replies:
    9
    Views:
    563
    Adrienne Boswell
    Mar 2, 2007
  5. IamIan
    Replies:
    14
    Views:
    864
    Istvan Albert
    Oct 22, 2007
Loading...

Share This Page