Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > javascript type casting nightmare IE7/firefox.

Reply
Thread Tools

javascript type casting nightmare IE7/firefox.

 
 
The Natural Philosopher
Guest
Posts: n/a
 
      11-22-2008
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.
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      11-22-2008
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)
 
Reply With Quote
 
 
 
 
The Natural Philosopher
Guest
Posts: n/a
 
      11-22-2008
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.



 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      11-22-2008
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
 
Reply With Quote
 
The Natural Philosopher
Guest
Posts: n/a
 
      11-22-2008
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.

}
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      11-22-2008
On Nov 22, 11:05*am, The Natural Philosopher <(E-Mail Removed)> 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]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting primitive type to user-defined type works in usage xllx.relient.xllx@gmail.com C++ 2 04-15-2006 05:37 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM
Casting between const type** and type** Kevin L C++ 6 08-11-2003 03:39 PM



Advertisments