Connecting Tech Pros Worldwide Help | Site Map

Snowflake code works in IE and Opera, but not in Firefox and Safari

Newbie
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 3
#1: Dec 13 '07
This is a java script code that will make it snow on your page. My problem is that it works just fine in IE and Opera, but doesn't work in Fire Fox or Safari. Here is the code. If you have any idea why i can't get it to work that would be great!

Expand|Select|Wrap|Line Numbers
  1. <!-- this script got from www.javascriptfreecode.com-Coded by: Krishna Eydatoula -->
  2. <!-- Web Site:  http://www.javakhafan.9f.com-->
  3. <!-- javakhafan.9f.com -->
  4. <script>
  5. // Set the number of snowflakes (more than 30 - 40 not recommended)
  6. var snowmax=35
  7.  
  8. // Set the colors for the snow. Add as many colors as you like
  9. var snowcolor=new Array("#aaaacc","#ddddFF","#ccccDD")
  10.  
  11. // Set the fonts, that create the snowflakes. Add as many fonts as you like
  12. var snowtype=new Array("Arial Black","Arial Narrow","Times","Comic Sans MS")
  13.  
  14. // Set the letter that creates your snowflake (recommended:*)
  15. var snowletter="*"
  16.  
  17. // Set the speed of sinking (recommended values range from 0.3 to 2)
  18. var sinkspeed=0.6
  19.  
  20. // Set the maximal-size of your snowflaxes
  21. var snowmaxsize=22
  22.  
  23. // Set the minimal-size of your snowflaxes
  24. var snowminsize=8
  25.  
  26. // Set the snowing-zone
  27. // Set 1 for all-over-snowing, set 2 for left-side-snowing 
  28. // Set 3 for center-snowing, set 4 for right-side-snowing
  29. var snowingzone=3
  30.  
  31. ///////////////////////////////////////////////////////////////////////////
  32. // CONFIGURATION ENDS HERE
  33. ///////////////////////////////////////////////////////////////////////////
  34.  
  35.  
  36. // Do not edit below this line
  37. var snow=new Array()
  38. var marginbottom
  39. var marginright
  40. var timer
  41. var i_snow=0
  42. var x_mv=new Array();
  43. var crds=new Array();
  44. var lftrght=new Array();
  45. var browserinfos=navigator.userAgent 
  46. var ie5=document.all&&document.getElementById&&!browserinfos.match(/Opera/)
  47. var ns6=document.getElementById&&!document.all
  48. var opera=browserinfos.match(/Opera/)  
  49. var browserok=ie5||ns6||opera
  50.  
  51. function randommaker(range) {        
  52.     rand=Math.floor(range*Math.random())
  53.     return rand
  54. }
  55.  
  56. function initsnow() {
  57.     if (ie5 || opera) {
  58.         marginbottom = document.body.clientHeight
  59.         marginright = document.body.clientWidth
  60.     }
  61.     else if (ns6) {
  62.         marginbottom = window.innerHeight
  63.         marginright = window.innerWidth
  64.     }
  65.     var snowsizerange=snowmaxsize-snowminsize
  66.     for (i=0;i<=snowmax;i++) {
  67.         crds[i] = 0;                      
  68.         lftrght[i] = Math.random()*15;         
  69.         x_mv[i] = 0.03 + Math.random()/10;
  70.         snow[i]=document.getElementById("s"+i)
  71.         snow[i].style.fontFamily=snowtype[randommaker(snowtype.length)]
  72.         snow[i].size=randommaker(snowsizerange)+snowminsize
  73.         snow[i].style.fontSize=snow[i].size
  74.         snow[i].style.color=snowcolor[randommaker(snowcolor.length)]
  75.         snow[i].sink=sinkspeed*snow[i].size/5
  76.         if (snowingzone==1) {snow[i].posx=randommaker(marginright-snow[i].size)}
  77.         if (snowingzone==2) {snow[i].posx=randommaker(marginright/2-snow[i].size)}
  78.         if (snowingzone==3) {snow[i].posx=randommaker(marginright/2-snow[i].size)+marginright/4}
  79.         if (snowingzone==4) {snow[i].posx=randommaker(marginright/2-snow[i].size)+marginright/2}
  80.         snow[i].posy=randommaker(2*marginbottom-marginbottom-2*snow[i].size)
  81.         snow[i].style.left=snow[i].posx
  82.         snow[i].style.top=snow[i].posy
  83.     }
  84.     movesnow()
  85. }
  86.  
  87. function movesnow() {
  88.     for (i=0;i<=snowmax;i++) {
  89.         crds[i] += x_mv[i];
  90.         snow[i].posy+=snow[i].sink
  91.         snow[i].style.left=snow[i].posx+lftrght[i]*Math.sin(crds[i]);
  92.         snow[i].style.top=snow[i].posy
  93.  
  94.         if (snow[i].posy>=marginbottom-2*snow[i].size || parseInt(snow[i].style.left)>(marginright-3*lftrght[i])){
  95.             if (snowingzone==1) {snow[i].posx=randommaker(marginright-snow[i].size)}
  96.             if (snowingzone==2) {snow[i].posx=randommaker(marginright/2-snow[i].size)}
  97.             if (snowingzone==3) {snow[i].posx=randommaker(marginright/2-snow[i].size)+marginright/4}
  98.             if (snowingzone==4) {snow[i].posx=randommaker(marginright/2-snow[i].size)+marginright/2}
  99.             snow[i].posy=0
  100.         }
  101.     }
  102.     var timer=setTimeout("movesnow()",50)
  103. }
  104.  
  105. for (i=0;i<=snowmax;i++) {
  106.     document.write("<span id='s"+i+"' style='position:absolute;top:-"+snowmaxsize+"'>"+snowletter+"</span>")
  107. }
  108. if (browserok) {
  109.     window.onload=initsnow
  110. }
  111. </script>
Expand|Select|Wrap|Line Numbers
  1. <font face="Tahoma"><a target="_blank" href="http://www.javascriptfreecode.com/"><span style="font-size: 8pt; text-decoration: none">JavaScript Free Code</span></a></font>
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Dec 13 '07

re: Snowflake code works in IE and Opera, but not in Firefox and Safari


You do realize that this is a Java forum and Java and Javascript are two entirely
different things? I'll move your question over to the Javascript forum.

kind regards,

Jos
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#3: Dec 13 '07

re: Snowflake code works in IE and Opera, but not in Firefox and Safari


There are no properties called posx or posy in firefox or safari- and they haven't been used much in IE since IE4.
Newbie
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 3
#4: Dec 13 '07

re: Snowflake code works in IE and Opera, but not in Firefox and Safari


Quote:

Originally Posted by JosAH

You do realize that this is a Java forum and Java and Javascript are two entirely
different things? I'll move your question over to the Javascript forum.

kind regards,

Jos


LoL. Sorry, I knew that but just didn't catch that. Thanks
Newbie
 
Join Date: Dec 2007
Location: Oklahoma
Posts: 3
#5: Dec 13 '07

re: Snowflake code works in IE and Opera, but not in Firefox and Safari


I have fixed this up a lot but still have not got it to work on Firefox or Safari. It will work on IE and Opera. If you have advice on what else do do or what i might have missed that would be great!

Expand|Select|Wrap|Line Numbers
  1. // Set the number of snowflakes (more than 30 - 40 not recommended)
  2. var snowmax=40
  3.  
  4. // Set the colors for the snow. Add as many colors as you like
  5. var snowcolor=new Array("#aaaacc","#ddddFF","#ccccDD")
  6.  
  7. // Set the fonts, that create the snowflakes. Add as many fonts as you like
  8. var snowtype=new Array("Arial Black","Arial Narrow","Times","Comic Sans MS")
  9.  
  10. // Set the letter that creates your snowflake (recommended:*)
  11. var snowletter="*"
  12.  
  13. // Set the speed of sinking (recommended values range from 0.3 to 2)
  14. var sinkspeed=1.0
  15.  
  16. // Set the maximal-size of your snowflaxes
  17. var snowmaxsize=22
  18.  
  19. // Set the minimal-size of your snowflaxes
  20. var snowminsize=10
  21.  
  22. // Set the snowing-zone
  23. // Set 1 for all-over-snowing, set 2 for left-side-snowing 
  24. // Set 3 for center-snowing, set 4 for right-side-snowing
  25. var snowingzone=1
  26.  
  27. ///////////////////////////////////////////////////////////////////////////
  28. // CONFIGURATION ENDS HERE
  29. ///////////////////////////////////////////////////////////////////////////
  30.  
  31.  
  32. // Do not edit below this line
  33. var snow=new Array()
  34. var marginbottom
  35. var marginright
  36. var timer
  37. var i_snow=0
  38. var x_mv=new Array();
  39. var crds=new Array();
  40. var lftrght=new Array();
  41. var browserinfos=navigator.userAgent 
  42. var ie5=document.all&&document.getElementById&&!browserinfos.match(/Opera/)
  43. var ns6=document.getElementById&&!document.all
  44. var opera=browserinfos.match(/Opera/) 
  45. var safari=browserinfos.match(/Apple/)
  46. var mozilla=navigator.userAgent.match(/Mozilla/) 
  47. var browserok=ie5||ns6||opera||mozilla||safari
  48. browserok=true
  49.  
  50. function randommaker(range) {        
  51.     rand=Math.floor(range*Math.random())
  52.     return rand
  53. }
  54.  
  55. function initsnow() {
  56.         marginbottom=10
  57.         marginright=10
  58.     if (ie5 || opera) {
  59.         marginbottom = document.body.clientHeight
  60.         marginright = document.body.clientWidth
  61.     }
  62.     else if (ns6) {
  63.         marginbottom = window.innerHeight
  64.         marginright = window.innerWidth
  65.     }
  66.     var snowsizerange=snowmaxsize-snowminsize
  67.     for (i=0;i<=snowmax;i++) {
  68.         crds[i] = 0;                      
  69.         lftrght[i] = Math.random()*15;         
  70.         x_mv[i] = 0.03 + Math.random()/10;
  71.         snow[i]=document.getElementById("s"+i)
  72.         snow[i].style.fontFamily=snowtype[randommaker(snowtype.length)]
  73.         snow[i].size=randommaker(snowsizerange)+snowminsize
  74.         snow[i].style.fontSize=snow[i].size
  75.         snow[i].style.color=snowcolor[randommaker(snowcolor.length)]
  76.         snow[i].sink=sinkspeed*snow[i].size/5
  77.         if (snowingzone==1) {snow[i].x=randommaker(marginright-snow[i].size)}
  78.         if (snowingzone==2) {snow[i].x=randommaker(marginright/2-snow[i].size)}
  79.         if (snowingzone==3) {snow[i].x=randommaker(marginright/2-snow[i].size)+marginright/4}
  80.         if (snowingzone==4) {snow[i].x=randommaker(marginright/2-snow[i].size)+marginright/2}
  81.         snow[i].y=randommaker(2*marginbottom-marginbottom-2*snow[i].size)
  82.         snow[i].style.left=snow[i].x
  83.         snow[i].style.top=snow[i].y
  84.     }
  85.     movesnow()
  86. }
  87.  
  88. function movesnow() {
  89.     for (i=0;i<=snowmax;i++) {
  90.         crds[i] += x_mv[i];
  91.         snow[i].y+=snow[i].sink
  92.         snow[i].style.left=snow[i].x+lftrght[i]*Math.sin(crds[i]);
  93.         snow[i].style.top=snow[i].y
  94.  
  95.         if (snow[i].y>=marginbottom-2*snow[i].size || parseInt(snow[i].style.left)>(marginright-3*lftrght[i])){
  96.             if (snowingzone==1) {snow[i].x=randommaker(marginright-snow[i].size)}
  97.             if (snowingzone==2) {snow[i].x=randommaker(marginright/2-snow[i].size)}
  98.             if (snowingzone==3) {snow[i].x=randommaker(marginright/2-snow[i].size)+marginright/4}
  99.             if (snowingzone==4) {snow[i].x=randommaker(marginright/2-snow[i].size)+marginright/2}
  100.             snow[i].y=0
  101.         }
  102.     }
  103.     var timer=setTimeout("movesnow()",50)
  104. }
  105.  
  106. for (i=0;i<=snowmax;i++) {
  107.     document.write("<span id='s"+i+"' style='position:absolute;top:-"+snowmaxsize+"'>"+snowletter+"</span>")
  108. }
  109. if (browserok) {
  110.     window.onload=initsnow
  111. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Dec 13 '07

re: Snowflake code works in IE and Opera, but not in Firefox and Safari


I've merged the threads and changed the thread title. Makes everything a lot cleaner and easier.
Reply