javascript type casting nightmare IE7/firefox.

  • Thread starter The Natural Philosopher
  • Start date
T

The Natural Philosopher

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.
 
E

Evertjan.

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)

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

Wonder, o wonder, we agree!
 
T

The Natural Philosopher

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


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)



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.
 
S

SAM

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>
 
T

The Natural Philosopher

SAM said:
Le 11/22/08 3:36 PM, The Natural Philosopher a écrit :

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.

}
 
D

David Mark

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]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top