Results 1 to 5 of 5

Thread: Explaining the Script

  1. #1
    Join Date
    May 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Explaining the Script

    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
    Last edited by tech_support; 06-10-2007 at 05:56 AM. Reason: Someone's stupidity.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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.
    Last edited by tech_support; 06-10-2007 at 06:02 AM.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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.
    Code:
    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
    Code:
    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().

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    May 2007
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you all for the help and insight and answers that are reliable and hopefully correct.


    It is sincerely appreciated.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •