Why this JavaScript code isn't compatible on Firefox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    Why this JavaScript code isn't compatible on Firefox?

    I have a scrit from Dynamic Drive HTML, a site that allows to you to download scripts in JavaScript to use on your website. I script I have, has conversions to work in IE or Netscape. It doens't however work in Firefox and if it did, I could possible work in other browsers. I was wondering if anyone would be able to tell me what I need to change to get it to work in Firefox?

    Code:
    <html>
    <script language="JavaScript1.2">
    
    //3D Starfield - by Mikhail Ansolis, based on Flying planes - by John Ely
    //Modified from: Autumn leaves script- by Kurt Grigg (kurt.grigg@virgin.net)
    //Visit http://www.dynamicdrive.com for this script
    
    //Pre-load your image below!
    //CHANGE 5 to the number of images listed below
    grphcs=new Array(5) 
    
    //PRELOAD the involved images (extend or contract variables according to # of images used)
    Image0=new Image();
    Image0.src=grphcs[0]="Star0.gif"
    Image1=new Image();
    Image1.src=grphcs[1]="Star1.gif"
    Image2=new Image();
    Image2.src=grphcs[2]="Star2.gif"
    Image3=new Image();
    Image3.src=grphcs[3]="Star3.gif"
    Image4=new Image();
    Image4.src=grphcs[4]="Star4.gif"
    
    //SPECIFY number of images to randomly display concurrently from list above. Less the more efficient
    Amount=20; 
    Ypos=new Array();
    Xpos=new Array();
    Zpos=new Array();
    //SpeedX=new Array();
    //SpeedY=new Array();
    ScrXpos=new Array();
    ScrYpos=new Array();
    Speed=new Array();
    Size=new Array();
    Step=new Array();
    ns=(document.layers)?1:0;
    ie=document.all&&navigator.userAgent.indexOf("Opera")==-1
    
    if (ns){
    for (i = 0; i < Amount; i++){
     var P=Math.floor(Math.random()*grphcs.length);
     rndPic=grphcs[P];
     document.write("<LAYER NAME='sn"+i+"' LEFT=0 TOP=0><img src="+rndPic+"></LAYER>");
    } //end for
    } //end if
    else if (ie){ //IE
    document.write('<div style="position:absolute;top:0px;left:0px"><div style="position:relative">');
    for (i = 0; i < Amount; i++){
     document.write('<img id="si'+i+'" src="'+grphcs[Math.round((i+1)*(grphcs.length-1)/Amount)]+'"style="position:absolute;top:0px;left:0px">');
    }
    document.write('</div></div>');
    }
    
    //initialize star properties
    if (ie||ns){
    WinHeight=(ns)?window.innerHeight-20:window.document.body.clientHeight;
    WinWidth=(ns)?window.innerWidth-70:window.document.body.clientWidth;
    for (i=0; i < Amount; i++){
      Speed[i]=(i+1)*6/Amount+4; //speed range from 4 to 10
      Xpos[i] = (Math.random()*WinWidth-WinWidth/2)*(Amount-i/3)/Amount;
      Ypos[i] = (Math.random()*WinHeight-WinHeight/2)*(Amount-i/3)/Amount;
      Zpos[i] = Math.random()*900*(Amount-i/3)/Amount+100;
    }
    }
    
    function fly(){
    var WinHeight=(ns)?window.innerHeight-20:window.document.body.clientHeight;
    var WinWidth=(ns)?window.innerWidth-70:window.document.body.clientWidth;
    var hscrll=(ns)?window.pageYOffset:document.body.scrollTop;
    
    for (i=0; i < Amount; i++){
     Zpos[i]-=Speed[i];
     if (ScrXpos[i]>WinWidth || ScrXpos[i]<0 || ScrYpos[i]>WinHeight || ScrYpos[i]<0 || Zpos[i]<=0) { //generate new stars
      Speed[i]=(i+1)*6/Amount+4; //speed range from 4 to 10
      Xpos[i] = (Math.random()*WinWidth-WinWidth/2)*(Amount-i/3)/Amount;
      Ypos[i] = (Math.random()*WinHeight-WinHeight/2)*(Amount-i/3)/Amount;
      Zpos[i] = Math.random()*900*(Amount-i/3)/Amount+100;
     }
     ScrXpos[i]=Xpos[i]/Zpos[i]*100 + WinWidth/2;
     ScrYpos[i]=Ypos[i]/Zpos[i]*100 + WinHeight/2;
    
     if (ns){
      document.layers['sn'+i].left=ScrXpos[i];
      document.layers['sn'+i].top=ScrYpos[i]+hscrll;
     }
     else{ //IE
      eval("document.all.si"+i).style.left=ScrXpos[i];
      eval("document.all.si"+i).style.top=ScrYpos[i]+hscrll;
     }
    } //end for
    setTimeout('fly()',20);
    } //end fly
    
    if (ie||ns)
    window.onload=fly
    //-->
    </SCRIPT>
    
    <HEAD>
    <TITLE>Starfield</TITLE>
    </HEAD>
    
    </html>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    nowadays, no-one seriously considers using document.all and document.layers. document.getEle mentById() works in all current browsers I know.

    Comment

    • Logic Ali
      New Member
      • Jul 2010
      • 16

      #3
      I have a script from Dynamic Drive HTML, a site that allows to you to download scripts in JavaScript to use on your website.
      You really do need to stay away from there. Most of their code is badly written and outdated. That site obviously has an agenda, but peddling quality code isn't it.

      Comment

      • KeredDrahcir
        Contributor
        • Nov 2009
        • 426

        #4
        Yes. I have noticed their code isn't always to best quality but I frequently found it useful for what I needed. I don't go there often much more, I just had an new idea for a website and needed some extra effect and that was one thing I wanted to try.

        It may be worth letting them know it needs updating. Having code that has adjustments in it for Netscape is a big clue since that's been gone for several years now. I think I got my hands one it probably months before it died.

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          I got it working but taking out any if statements that said if (ie).
          It works on Firefix but both scroll bars keep appearing and disappearting which must be due to the window size but I can't change it. If I choose to have scroll bars there all the time, the box that allows you to scroll keeps appearing and disappearing. If I press F11 it only happens with the vertical scroll bar.

          Can anyone work out how to stop that happening?

          Comment

          Working...