global var javascript

Discussion in 'Javascript' started by Kalyke@gmail.com, Feb 29, 2008.

  1. Guest

    I think the below code should work, but for some reason the setNumbers
    variable isn't keeping its value after the function. I thought if I
    declared variables outside of any function they would behave globally,
    so if I put data into them in a function it would carry over out of
    the function. Where the alert is at the bottom works, but if its moved
    one block lower it gives me an undefined. All the var's are delcared
    in the main part of code. Can someone tell me what I'm doing wrong?


    Code:
    var setNum = 0;
    var imageArray = new Array();
    var srcArray = new Array();
    var altArray = new Array();
    var imageSet = new Array(); /////Declared Here outside of any function
    var setNumbers;
    var numXml;
    var xDocument;
    var xmlDoc;
    var counter=0;
    var cnt = 0;
    
    var URL = "pictures.xml";
    var request = false;
    if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try{
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){}
    }
    }
    request.open("GET", URL, true);
    request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
    xmlDoc = request.responseXML;
    xDocument=xmlDoc.getElementsByTagName("picture");
    numXml = xDocument.length;
    setNumbers = numXml / 4;
    
    for (i=0;i<numXml;i++){
    srcArray[i] = xmlDoc.getElementsByTagName("src")
    [i].firstChild.nodeValue;
    altArray[i] = xmlDoc.getElementsByTagName("alt")
    [i].firstChild.nodeValue;
    var img = "<img src=\'"+srcArray[i]+"\' onClick=\'replaceMain("+i
    +")\' onmouseover=\"this.style.cursor=\'pointer\'\" alt=\'"+altArray[i]
    +"\' width=144 height=108 >";
    imageArray[i] = "<li>"+img+"</li>";
    
    }
    //calls function fisherYates and puts new data into groups for display
    fisherYates(imageArray);
    while(counter < setNumbers) {
    imageSet[counter]=imageArray[cnt];
    cnt++;
    imageSet[counter]+=imageArray[cnt];
    cnt++;
    imageSet[counter]+=imageArray[cnt];
    cnt++;
    imageSet[counter]+=imageArray[cnt];
    cnt++;
    counter++;
    }
    alert(imageSet[2]); ///////////works here
    }
    alert(imageSet[2]); /////////doesn't work says "undefined"
    }
    request.send(null);
     
    , Feb 29, 2008
    #1
    1. Advertising

  2. On Fri, 29 Feb 2008 14:15:27 -0800, Kalyke wrote:

    > I think the below code should work, but for some reason the setNumbers
    > variable isn't keeping its value after the function. I thought if I
    > declared variables outside of any function they would behave globally,
    > so if I put data into them in a function it would carry over out of the
    > function. Where the alert is at the bottom works, but if its moved one
    > block lower it gives me an undefined. All the var's are delcared in the
    > main part of code. Can someone tell me what I'm doing wrong?


    There isn't enough information to answer that question without creating
    my own "pictures.xml" file.

    Post a link to a web site and I'll look again.

    (In the future, please change the indent to TWO spaces (NOT TABS) when
    posting on usenet.)
     
    Jeremy J Starcher, Feb 29, 2008
    #2
    1. Advertising

  3. wrote:
    > I think the below code should work, but for some reason the setNumbers
    > variable isn't keeping its value after the function.


    That only seems to be so.

    > I thought if I declared variables outside of any function they would
    > behave globally, so if I put data into them in a function it would carry
    > over out of the function.


    That is correct. However, the method containing the assignment would have
    to be called first.

    > Where the alert is at the bottom works, but if its moved one block lower
    > it gives me an undefined. All the var's are delcared in the main part of
    > code. Can someone tell me what I'm doing wrong?


    Most likely you are ignoring that the request is asynchronously handled,
    which is why the event listener would not have been called or the condition
    in it would not apply yet after

    request.send(null);

    >
    Code:
    [/color]
    
    Despite what Google Groups wants you to believe, this is not a Web forum,
    but a public Usenet newsgroup.  It is plain text only, so phpBB-style `code'
    tags have no effect and are unnecessary.  Indent your code properly instead,
    with two or four spaces instead of tabs, and post it so that automatic word
    wrap at 80 characters does not break it.  This will not only help people
    doing a helpful analysis without greater effort, but it will also help you
    as you can see more clearly what and where you are doing wrong.
    
    
    PointedEars
    --
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Mar 1, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,510
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    396
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    330
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Vinokur
    Replies:
    4
    Views:
    485
    Ron Natalie
    Sep 24, 2003
Loading...

Share This Page