PDA

View Full Version : Explaining the Script



skripter
06-10-2007, 04:51 AM
In my attemtp to learn more about coding I am now looking at working scripts and trying to understnad their code lines.

Often I encounter code lines that are new to me and do not understand.

The following is a partial script I extracted and am trying to understand:

I hope the script experts on this site may be able to answer any of the following questions.

<script id="script" type="text/javascript">
// <![CDATA[

why is the id="script" necessary?
What does the [CDATA[ mean? I haven't seen this before.

var speed=5; // lower number for faster

var flakes=150; // number of flakes

var colour="FFFAFA"; // colour of flakes

var slush=0; // set to '0' for no slush or otherwise set to height at which slush melts

/***************************\
\* DON'T EDIT BELOW THIS BOX *
\***************************/
var flks=new Array();
var flkx=new Array();
var flky=new Array();
var fldy=new Array();
var slss=new Array();
var slsh=new Array();
var swide, shigh;

6 variables were created or defined yet their elements were not specified. In all the tutorials I have seen online about arrays, they all showed that whenever an array is used their elements are also indicated. Where are the arrays' elements?

window.onload=function startsnowingdo() { if (document.getElementById) {
var b, s;
b=document.createElement("div");
s=b.style;
s.position="absolute";
b.setAttribute("id", "bod");
document.body.appendChild(b);
set_scroll();
set_width();
for (var i=0; i<flakes; i++) {
flks[i]=createDiv(1, 1);
flkx[i]=3*Math.floor(Math.random()*swide/3);
flky[i]=Math.floor(Math.random()*shigh);
fldy[i]=2+Math.floor(Math.random()*4);
flks[i].style.left=flkx[i]+"px";
flks[i].style.top=flky[i]+"px";
b.appendChild(flks[i]);
}

Can you explain please what this function does or accomplishes? Are they defining or creating the arrays' elements within this function?



setInterval("let_it_snow()", speed);
}}
function createDiv(height, width) {
var div=document.createElement("div");//Are they creating a div element? div.style.position="absolute";
div.style.height=height+"px";
div.style.width=width+"px";
div.style.overflow="hidden";
div.style.backgroundColor=colour;
return (div);
}

what does this function accomplish? What value does it return?

Thanks much

tech_support
06-10-2007, 05:56 AM
Please don't make your font sizes so big. We're not blind. We told you before already.



why is the id="script" necessary?
What does the [CDATA[ mean? I haven't seen this before.
I don't know. Can you please link to the script? Or your page? It might be required. But, in 99.99& of all cases, it's not neccessary.

6 variables were created or defined yet their elements were not specified. In all the tutorials I have seen online about arrays, they all showed that whenever an array is used their elements are also indicated. Where are the arrays' elements?
Probably for the functioning of the script.


Can you explain please what this function does or accomplishes? Are they defining or creating the arrays' elements within this function?
It creates snow flakes. (aka. moving div's)


var div=document.createElement("div");//Are they creating a div element? div.style.position="absolute";
Yup.


what does this function accomplish? What value does it return?
It creates a div with a specified width and height.

Trinithis
06-10-2007, 06:02 AM
why is the id="script" necessary?
What does the [CDATA[ mean? I haven't seen this before.
The id is probably not necessary. The cdata is in a comment (// = end of line comment).



6 variables were created or defined yet their elements were not specified. In all the tutorials I have seen online about arrays, they all showed that whenever an array is used their elements are also indicated. Where are the arrays' elements?
The following for loop adds to the arrays. If the value at i does not exist, then the index value is created.


for (var i=0; i<flakes; i++) {
flks[i]=createDiv(1, 1);
flkx[i]=3*Math.floor(Math.random()*swide/3);
flky[i]=Math.floor(Math.random()*shigh);
fldy[i]=2+Math.floor(Math.random()*4);
flks[i].style.left=flkx[i]+"px";
flks[i].style.top=flky[i]+"px";
b.appendChild(flks[i]);
}




var div=document.createElement("div");//Are they creating a div element?

Yes, but it does not add it to the page. You would have to use parent.appendChild() or something similar to add it the the page. In this case, the author does it by


flks[i]=createDiv(1, 1);
[ . . . ]
b.appendChild(flks[i]);


The code does not really do much because some of it is missing, such as the function let_it_snow().

Twey
06-10-2007, 02:24 PM
What does the [CDATA[ mean? I haven't seen this before.It marks the section as being character data, so it isn't parsed by the HTML or XML parser. It's the modern equivalent of using (SG|X)ML comment markers, <!-- -->, to safely embed the code without the chance of it being accidentally parsed as markup. Using XML comment markers in XHTML will cause the entire block to be ignored.

skripter
06-17-2007, 08:27 AM
Thank you all for the help and insight and answers that are reliable and hopefully correct.


It is sincerely appreciated.