Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross Browser Javascript

Status
Not open for further replies.

pneffkell

Technical User
Jun 21, 2001
1
US
I am new to Javascript and have run into a but of a problem. I am hoping someone can tell me what i am doing wrong.

I have all my code working for IE only. I am now trying to make it support both ie and ns. It is a simple layer animation that allows the programmer to feed in various variables to control the movement.

I move the layer in IE with this code

document.all(layerToMove).style.left = position

layerToMove is a variable

when I to do the tradition cross browser conversion of putting document.all and .style into a variable and then using eval to create the statement I get an error saying it does not recognize the layer denoted in the layerToMove variable

is am using this line of code

eval(layerRef + "(" + layerToMove + ")" + styleRef + ".left =" + position);


i am sure i am just missing something simple but I am lost with this one.




the full code is below

function moveIt()
{
moveLinks(1,0,500,1,4,50);//Link Number,Starting Position, Ending Position, Direction (1= Right 2= Left), Speed
moveLinks(2,800,300,2,4,50)
moveLinks(3,0,500,1,4,50)
moveLinks(4,800,300,2,4,50)
}
function moveLinks (linkNum,startPos,endPos,direction,speed,layerBounce)
{
//Cross Browser Conversion
layerRef = ""
styleRef = ""
if (navigator.appName =="Netscape")
{
layerRef ="document.layers"
styleRef =""
}
else
{
layerRef = "document.all"
styleRef = ".style"
}
// linkNum is the number of the Layer to be moved i.e. a Layer with the ID of link one would have a linkNum of 1
// startPos should be 0 or higher. Please do not use negative numbers
// endPos is the position where you would like for the layer to end before the bounce action (this is the final position
// + or - the bounce
// direction tell the code what direction you want it to move the layer. 1 is for right 2 is for left
// speed tells the code how many pixels to move the layer per iteration
// layerBounce is the number of pixels you would like the layer to bounce back when the code reaches endPos
loopLength = Math.abs(startPos - endPos);
position = startPos
layerToMove = "link" + linkNum
if (position != endPos)
{
if (direction == 1)
{
position =position + speed;
}
else// direction = 2
{
position = position - speed;
}
}
else
{
if (direction == 1)
{bounceBack = position - layerBounce}//bounce back is the number of pixels to bounce back from end of move
else
{bounceBack = position + layerBounce}//bounce back is the number of pixels to bounce back from end of move
bounce(linkNum,position,bounceBack,direction)
return
}
eval(layerRef + "(" + layerToMove + ")" + styleRef + ".left =" + position);
setTimeout('moveLinks('+linkNum+','+position+','+endPos+','+direction+','+speed+','+layerBounce+')',1);
}
function bounce(layerNum,bounceStart,bounceEnd,direction)
{
layerToBounce = "link" + layerNum
bouncePos = bounceStart
if (bouncePos != bounceEnd)
{
if (direction == 1)
{bouncePos = bouncePos - 1}
else
{bouncePos = bouncePos + 1}
}
else
{return}
document.all(layerToBounce).style.left = bouncePos
setTimeout('bounce('+layerNum+','+bouncePos+','+bounceEnd+','+direction+')',1);
}
 
hi
try smth like this:
DOM=(document.getElementById)?true:false
NC4=(document.layers)?true:false
IE=(document.all)?true:false
NC6=DOM && !IE
IE5=DOM && IE
NC=NC4 || NC6

ImgPre="document.images[\""
styler=".style"
LayerPost="\"]"
ImgPost=LayerPost

if(NC4){
LayerPre="document.layers[\""
styler=""
}

if(IE){
LayerPre="document.all[\""
}

if(NC6||IE5){
LayerPre="document.getElementById(\""
ImgPre=LayerPre
LayerPost="\")"
ImgPost=LayerPost
}


and then
var obj=eval(LayerPre+'newLayer'+LayerPost+styler)
obj.left=15999

in your example u use ("") in refs may be this is your mistake

i get this stuff 2 work in ie5.0 & nn4.5+

regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top