Forum Moderators: open

Message Too Old, No Replies

Cross Browser for Netscape 7.1 not working

CSS and JavaScript

         

classicalmusic

9:20 am on Jun 1, 2004 (gmt 0)

10+ Year Member



Hi,

I have been trying to script this exercise so that it will work in Netscape 7.1. As seen in my example below, I have tried to do this by incorporating the getElementById method but without success. Could someone please help.

function flyingsleigh()
{
if (document.all) // Microsoft Explorer 6.0
{
reindeer.style.pixelLeft +=5
if (reindeer.style.pixelLeft > 800)
{
reindeer.style.pixelLeft = -100
}
spot=setTimeout("flyingsleigh()",50)
}

if (document.layers) // Netscape 4.73
{
document.reindeer.left += 5

if (document.reindeer.left > 800)
{
document.reindeer.left = -100
}

spot=setTimeout("flyingsleigh()",50)

}

if (document.getElementById) // Netscape 7.1
{
document.getElementById('reindeer').pixelLeft += 5

if (document.getElementById('reindeer').pixelLeft > 800)
{
document.getElementById('reindeer').pixelLeft = -100
}

spot=setTimeout("flyingsleigh()",50)

}
}

function stopReindeer()
{

if (document.all) // Explorer
{
clearTimeout(spot)
reindeer.style.pixelLeft =20
}

if (document.layers) // Netscape 4.73
{
clearTimeout(spot)
document.reindeer.left =20

}

if(document.getElementById) // Netscape 7.1
{
clearTimeout(spot)
document.getElementById.pixelLeft =20

}

}
// -->
</script>

Rambo Tribble

12:48 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



pixelLeft is an IE-only property. Your syntax for NS (and all standards-compliant browsers, including later IE) should be:

document.getElementById("ID_name").style.left

Obviously, you substitute the element's actual id for the ID_name in the above.

By the way, the value you assign to the style.left should be a string, in quotes, with a measurement value, like "20px".

DrDoc

3:43 pm on Jun 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also need to use proper
else if
syntax, starting with getElementById for browsers that support it (which includes newer IE):

if(document.getElementById) {
//do stuff
}
else if(document.all) {
//do stuff
}
else if(document.layers) {
//do stuff
}

classicalmusic

9:45 am on Jun 2, 2004 (gmt 0)

10+ Year Member



Many thanks for your help. Please excuse me if I sound a bit “thick” but I am a beginner to JavaScript and am still trying to learn the basics.

I am now receiving a JS error saying Invalid argument on line: 19 char: 54, which is the next line following if declaration for NS 7.1.

Thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<style type="text/css">
<!--
#reindeer {position:relative; left:20px; top:20px}
-->
</style>
<title>Cross Browser</title>
<script type="text/javascript">
<!--
function flyingsleigh()
{

if (document.getElementById) // Netscape 7.1
{

document.getElementById('reindeer').style.left += "5"

if (document.getElementById('reindeer').style.left > "800")
{
document.getElementById('reindeer').style.left = -"100"
}

spot=setTimeout("flyingsleigh()","50")

}

else if (document.all) // Microsoft Explorer 6.0
{
reindeer.style.pixelLeft +=5
if (reindeer.style.pixelLeft > 800)
{
reindeer.style.pixelLeft = -100
}
spot=setTimeout("flyingsleigh()",50)
}

else if (document.layers) // Netscape 4.73
{
document.reindeer.left += 5

if (document.reindeer.left > 800)
{
document.reindeer.left = -100
}

spot=setTimeout("flyingsleigh()",50)

}

}

// -->
</script>
</head>
<body bgcolor="#ffffff">
<DIV ID="reindeer"><img src="sleigh.gif">
</DIV>
<FORM>
<INPUT TYPE="button" VALUE="Fly Reindeer" onClick="flyingsleigh()">
<P></P>

</FORM>
</body>
</html>