简体   繁体   中英

How do i make a Javascript array global?

Forgive me I need a little help, I don't have a coders brain, actually i'm impressed I got this far but now I need a little help.

I'm trying to build a player to load html files into a div from an xml file and then be able to navigate through the pages with previous and next buttons. So far I have managed to load the xml, convert it to an array and load the first page but when I click on the previous and next buttons I get "pages is undefined".

Here is the code:

$(document).ready(function(){

    var i = 0;

    $.ajax({
        type: "GET",
        url: "studyguide/new_course.xml",
        dataType: "xml",
        success: function(xml) {          
            pages = parseXml(xml)
            doStuff(pages);
            loadFirstPage(pages);
        } // END success
    }); //END ajax

    function parseXml(xml) {
        var pages = [];
        $(xml).find("page").each(function() {
            pages.push({
                title: $(this).find("title").text(), 
                url: $(this).find("url").text()
            }); // END .push
        }); // END .each
        return pages;
    } // END parseXML

    function doStuff(pages) {
        //Do some javascript stuff with the response
        alert(pages[0].title);
        alert(pages[0].url);
    } // END doStuff

    function loadFirstPage(pages){
        //alert(pages[i].url);
        $('#displayResults').html('<img src="../images/495.gif" />');
        $( "#displayResults" ).load(pages[i].url, function() {
        }) //END .load
    }; //END loadFirstPage

    function loadPage(pages){
        //alert(pages[i].url);
        $('#displayResults').html('<img src="../images/495.gif" />');
        $( "#displayResults" ).load(pages[i].url, function() {
        }) //END .load
    };// END loadPage

    $('#prev').bind('click', function(pages) {
        i--;
        //alert(i);
        loadPage();
    }) //END click

    $('#next').bind('click', function(pages) {
        i++;
        //alert(i);
        loadPage();
    }) // END click

}); // END ready

And the xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<course>
    <section name = "Section One">
        <page>
            <title>Page 1</title>
            <url>studyguide/page1.html</url> 
            <instructions></instructions>   
        </page> 
        <page>
            <title>Page 2</title>
            <url>studyguide/page2.html</url> 
            <instructions></instructions>   
        </page>
        <page>
            <title>Page 3</title>
            <url>studyguide/page3.html</url> 
            <instructions></instructions>   
        </page>
    </section>
    <section name = "Section Two">
        <page>
            <title>Page 1</title>
            <url>studyguide/page4.html</url> 
            <instructions></instructions>   
        </page>
        <page>
            <title>Page 2</title>
            <url>studyguide/page5.html</url> 
            <instructions></instructions>   
        </page>
    </section>
</course>

So I have 2 Questions, firstly how do I make the array global so it can be used by the loadPage function. And second, is there a better way of doing this in the first place. The xml could contain 5 pages or 100 pages so I want to keep it dynamic.

Thanks

Thanks for your help guys. I made some changes and now I get "pages[I] is undefined. Sorry i'm not a coder by nature so you have to go slowly with me.

$(document).ready(function(){

    var i = 0;
    var pages = [];

    $.ajax({
        type: "GET",
        url: "studyguide/new_course.xml",
        dataType: "xml",
        success: function(xml) {          
            pages = parseXml(xml)
            doStuff(pages);
            loadFirstPage(pages);
        } // END success
    }); //END ajax

    function parseXml(xml) {
        //var pages = [];
        $(xml).find("page").each(function() {
            pages.push({
                title: $(this).find("title").text(), 
                url: $(this).find("url").text()
            }); // END .push
        }); // END .each
        return pages;
    } // END parseXML

    function doStuff(pages) {
        //Do some javascript stuff with the response
        alert(pages[0].title);
        alert(pages[0].url);
    } // END doStuff

    function loadFirstPage(pages){
        //alert(pages[i].url);
        $('#displayResults').html('<img src="../images/495.gif" />');
        $( "#displayResults" ).load(pages[i].url, function() {
        }) //END .load
    }; //END loadFirstPage

    function loadPage(pages){
        //alert(pages[i].url);
        $('#displayResults').html('<img src="../images/495.gif" />');
        $( "#displayResults" ).load(pages[i].url, function() {
        }) //END .load
    };// END loadPage

    $('#prev').bind('click', function(pages) {
        i--;
        //alert(i);
        loadPage(pages);
    }) //END click

    $('#next').bind('click', function(pages) {
        i++;
        alert(i);
        loadPage(pages);
    }) // END click

}); // END ready

To answer the first question, if you don't put a var in front of the variable declaration it will become global (on the window object). Doing this is generally considered a bad idea. I would personally probably declare the variable in your ready function then use it for your other functions within that scope.

Another observation here is that you set up your loadPage function with a parameter, however then you aren't passing it anything in your call to it.

I think your approach works just fine though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM