Forum Moderators: open

Message Too Old, No Replies

Cross-browser scripting for Netscape 4.73 and 7.1

         

classicalmusic

10:57 am on Jun 4, 2004 (gmt 0)

10+ Year Member



Hi Forum Members,

I am trying out a simple exercise to teach myself how to script Style Sheet layers so that they will work in Netscape 4.73 and Netscape 7.1.

The script itself is very simple – push the “SHOW” form button that executes a function called Appear(), using the onClick event handler and a box 250px width, with a 2px blue solid appears 600px from left and 170px down from the top of screen.

I have been able to get the script to work in Microsoft Explorer. But I cannot get it to work in the two Netscape browsers. In NS 4.73, I get a JS error, but this browser does not allow me to see where the error is occurring. In NS 7.1, there is no error, but the box does not show either.

I am very keen to come to grips with this CSS cross-browser scripting.

Could someone please tell me how I can get the Netscape browsers to work.

Many thanks.

Robin
New Zealand.

My coding appears below:

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

<html>
<head>
<style type="text/css">
<!--
#slide {position:relative; top:170px; left:600px; width:250px; z-index:1; border:2px blue solid; visibility:hidden}
-->
</style>

<script type="text/javascript">
<!--
function Appear()
{

// Explorer
if(document.all)

{
document.all['slide'].style.visibility='visible';
}

if (slide.style.pixelLeft > 600)
{
slide.style.pixelLeft = 600
}

//Netscape 4.73
if (document.layers)
{
document.layers('slide').style.visibility='visible'
}
if (document.layers)
{
document.layers('slide').style.left > 600
}

//Netscape 7.1
if (document.getElementById)
{
document.getElementById('slide').style.visibility='visible';
}
document.getElementById('slide').left > 600

}

// -->
</script>

<title>Layers</title>
</head>
<body>
<div id="slide">This is a test box
</div>
<form>
<input type="button" value="SHOW" onClick="Appear()">
</form>
<P></P>

</form>
</body>
</html>

Bernard Marx

1:17 pm on Jun 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You won't be able to get element style values that have been set by a stylesheet. You need to either set them by script, or put the ones you want to get into an inline style:

<div style="left:200px....">

This script has a section at the top that will sort things out a bit for you. It can be developed to make general scripting easier. It does mean that you have to follow certain conventions, like using

[blue]HIDDEN[/blue]
instead of
[blue]"hidden"[/blue]
. It doesn't use IE only things like
[blue]pixelLeft[/blue]
.

notes
[1]
NS4 elements have no

[blue]style[/blue]
object. My solution is to give the element a style property that points back to the element. It's a little inefficient to put it on the new '
[blue]get[/blue]
' method...but handy!
[2]
NS4 uses the values
[blue]"show"[/blue]
and
[blue]"hide"[/blue]
instead of
[blue]"visible"[/blue]
and
[blue]"hidden"[/blue]


// =========================================================== //
var isIE4 = (document.all &&!document.getElementById)? 1:0
var isNS4 = document.layers? 1:0
var isDOM = document.getElementById? 1:0

// give browser a getElementById method
// Explorer 4 : general
// Netscape 4 : still won't get [b]nested[/b] layers (can be improved)
if(isIE4)
document.getElementById = function(id){ return this.all[id] }
else if(isNS4)
{
document.getElementById = function(id)
{
var elm = this.layers(id)
elm.style = elm // note [1]
return elm
}
}
// sort out CSS values notes[2]
var VISIBLE: (isNS4)?"show":"visible",
var HIDDEN : (isNS4)?"hide":"hidden"

// =========================================================== //

function Appear()
{

// put element into local var
var slide = document.getElementById("slide")
// put elm.style into local too
varslideStyle = slide.style
if(parseInt(slideStyle.left) > 600)
slideStyle.visibility = VISIBLE
}

Rambo Tribble

5:04 am on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, you can get the currently applied style on an element. The method differs between IE and NS; won't work in older browsers and Opera. Also varies in the result returned for some attributes. Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Style Check</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
div.a{font-size:2em;}
</style>
<script type="text/javascript">
function showSty(){
if(document.all){
var s=document.getElementById("showOne");
alert(s.currentStyle.fontSize);
}else{
var s=getComputedStyle(document.getElementById('showOne'),'');
alert(s.getPropertyValue("font-size"));
}
}
</script>
</head>
<body>
<div id="showOne" class="a"
onclick="showSty();">
The div
</div>
</body>
</html>

Bernard Marx

9:30 am on Jun 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. What I really meant was "you can't, unless blah blah ...". That blah would include the fact that it won't happen in NS4, so we might as well not go there!

classicalmusic

10:12 am on Jun 5, 2004 (gmt 0)

10+ Year Member



Many thanks.

I am still experimenting with the script.

Robin.